def main(headless): port = STATE.get("PREFERRED_PORT") if not port: port = 8000 httpd = http.server.HTTPServer( ("", port), CohortManagerRequestHandler ) if STATE["DEBUG"] or headless: print("Server listening on port {}.".format(port)) # Launch the server thread. server = threading.Thread(target=httpd.serve_forever, daemon=False) server.start() if headless: # In headless mode, the only thing that the main thread will do is # to wait for interrupt. try: while server.is_alive(): pass except: httpd.shutdown() return else: try: client(port) except EOFError: print("YO") httpd.shutdown() except KeyboardInterrupt: print("Ya") httpd.shutdown() server.join()
def initialize(): global server if server is not None and server.is_alive(): return server = HTTPServer()