Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Created September 1, 2014 10:45
Show Gist options
  • Save jorge-lavin/8d29527265f7395717a8 to your computer and use it in GitHub Desktop.
Save jorge-lavin/8d29527265f7395717a8 to your computer and use it in GitHub Desktop.
import BaseHTTPServer
def keep_running():
user_keystroke = raw_input()
if user_keystroke == 'q':
return False
else:
return True
def run_while_true(server_class=BaseHTTPServer.HTTPServer,
handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
"""
This assumes that keep_running() is a function of no arguments which
is tested initially and after each request. If its return value
is true, the server continues.
"""
server_address = ('', 8000)
httpd = server_class(server_address, handler_class)
print('Serving at port 8000, press q and ENTER to stop server')
while keep_running():
httpd.handle_request()
if __name__ == '__main__':
run_while_true()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment