class HTTPServer(object): """ A simple http wrapper """ def __init__(self, host, port, processes, logfile, verbose, work_dir): self.host = host self.port = port self.processes = processes self.logfile = logfile self.verbose = verbose self.work_dir = work_dir setup_logging(self.logfile, self.verbose, self.work_dir) self.server = WSGIServer((self.host, self.port), app, backlog=100000, log=logger) self.server.init_socket() def serve_forever(self): self.server.start_accepting() self.server._stop_event.wait() gevent.wait() def start(self): for i in range(self.processes): p = Process(target=self.serve_forever, args=tuple()) p.start() PROCESSES.append(p) try: print "Start listener" for p in PROCESSES: p.join() except KeyboardInterrupt: for p in PROCESSES: print "stopping %s" % p.name p.terminate() except: pass finally: print "Done" sys.exit(0)
def _run_gevent(app, config, mode): """Run WsgiDAV using gevent if gevent is installed. See https://github.com/gevent/gevent/blob/master/src/gevent/pywsgi.py#L1356 https://github.com/gevent/gevent/blob/master/src/gevent/server.py#L38 for more options """ import gevent import gevent.monkey gevent.monkey.patch_all() from gevent.pywsgi import WSGIServer server_args = {"bind_addr": (config["host"], config["port"]), "wsgi_app": app} server_name = "WsgiDAV/{} gevent/{} Python/{}".format( __version__, gevent.__version__, util.PYTHON_VERSION ) # Support SSL ssl_certificate = _get_checked_path(config.get("ssl_certificate"), config) ssl_private_key = _get_checked_path(config.get("ssl_private_key"), config) ssl_certificate_chain = _get_checked_path( config.get("ssl_certificate_chain"), config ) # Override or add custom args server_args.update(config.get("server_args", {})) protocol = "http" if ssl_certificate: assert ssl_private_key protocol = "https" _logger.info("SSL / HTTPS enabled.") dav_server = WSGIServer( server_args["bind_addr"], app, keyfile=ssl_private_key, certfile=ssl_certificate, ca_certs=ssl_certificate_chain, ) else: dav_server = WSGIServer(server_args["bind_addr"], app) # If the caller passed a startup event, monkey patch the server to set it # when the request handler loop is entered startup_event = config.get("startup_event") if startup_event: def _patched_start(): dav_server.start_accepting = org_start # undo the monkey patch org_start() _logger.info("gevent is ready") startup_event.set() org_start = dav_server.start_accepting dav_server.start_accepting = _patched_start _logger.info("Running {}".format(server_name)) _logger.info( "Serving on {}://{}:{} ...".format(protocol, config["host"], config["port"]) ) try: gevent.spawn(dav_server.serve_forever()) except KeyboardInterrupt: _logger.warning("Caught Ctrl-C, shutting down...") return
from qb_server import app if __name__ == '__main__': import sys if len(sys.argv) == 2 and sys.argv[1] == 'prod': from gevent.pywsgi import WSGIServer http_server = WSGIServer(('localhost', 6000), app) http_server.start() http_server.start_accepting() http_server._stop_event.wait() else: app.run(debug=True, host='localhost', port=6000)