class HTTPServer: def __init__(self, port=8090): self.mappings = {} self.port = port self.server = CherryPyWSGIServer(("0.0.0.0", port), self, server_name="localhost") self.started = False self.t = threading.Thread(target=self._start) self.t.start() time.sleep(0.1) def _start(self): try: self.server.start() except Exception, e: print 'ERROR: failed to start server', str(e)
class HTTPServer: def __init__(self, port=8090): self.mappings = {} self.port = port self.server = CherryPyWSGIServer(("0.0.0.0", port), self, server_name="localhost") self.started = False self.t = threading.Thread(target=self._start) self.t.start() time.sleep(0.1) def _start(self): try: self.server.start() except Exception, e: print 'ERROR: failed to start server', str(e)
class HTTPServer: def __init__(self, port=8090): self.mappings = {} self.port = port self.server = CherryPyWSGIServer(("0.0.0.0", port), self, server_name="localhost") self.started = False self.t = threading.Thread(target=self._start) self.t.start() time.sleep(0.1) def _start(self): try: self.server.start() except Exception as e: print('ERROR: failed to start server', str(e)) def stop(self): self.server.stop() self.t.join() def request(self, path, method='GET', query={}): response = Respose() if isinstance(query, dict): query = urllib.parse.urlencode(query) self.mappings[path, method, query] = response return response def __call__(self, environ, start_response): _method = environ.get('REQUEST_METHOD', 'GET') _path = environ.get('PATH_INFO') for (path, method, query_string), response in self.mappings.items(): if _path == path and _method == method: return response(start_response) return Respose()(start_response)
class HTTPServer: def __init__(self, port=8090): self.mappings = {} self.port = port self.server = CherryPyWSGIServer(("0.0.0.0", port), self, server_name="localhost") self.started = False self.t = threading.Thread(target=self._start) self.t.start() time.sleep(0.1) def _start(self): try: self.server.start() except Exception as e: print('ERROR: failed to start server', str(e)) def stop(self): self.server.stop() self.t.join() def request(self, path, method='GET', query={}): response = Respose() if isinstance(query, dict): query = urllib.urlencode(query) self.mappings[path, method, query] = response return response def __call__(self, environ, start_response): _method = environ.get('REQUEST_METHOD', 'GET') _path = environ.get('PATH_INFO') for (path, method, query_string), response in self.mappings.items(): if _path == path and _method == method: return response(start_response) return Respose()(start_response)
if __name__ == "__main__": def usage(): print "Usage: %s [{cherrypy|gevent|fapws3} PORT]" % argv[0] if len(argv) > 2: sname = argv[1] port = int(argv[2]) if sname == "cherrypy": from web.wsgiserver import CherryPyWSGIServer as WSGIServer server = WSGIServer(("0.0.0.0", port), wsgifunc, request_queue_size=40) try: server.start() except KeyboardInterrupt: server.stop() elif sname == "gevent": from gevent.wsgi import WSGIServer server = WSGIServer(("0.0.0.0", port), wsgifunc, log=None) server.serve_forever() elif sname == "fapws3": import fapws._evwsgi as evwsgi from fapws import base evwsgi.start("0.0.0.0", str(port)) evwsgi.set_base_module(base) evwsgi.wsgi_cb(("", wsgifunc)) evwsgi.set_debug(0) evwsgi.run() else:
wsgifunc = app.wsgifunc() if __name__ == "__main__": def usage(): print "Usage: %s [{cherrypy|gevent|fapws3} PORT]" % argv[0] if len(argv) > 2: sname = argv[1] port = int(argv[2]) if sname == "cherrypy": from web.wsgiserver import CherryPyWSGIServer as WSGIServer server = WSGIServer(("0.0.0.0", port), wsgifunc, request_queue_size=40) try: server.start() except KeyboardInterrupt: server.stop() elif sname == "gevent": from gevent.wsgi import WSGIServer server = WSGIServer(("0.0.0.0", port), wsgifunc, log=None) server.serve_forever() elif sname == "fapws3": import fapws._evwsgi as evwsgi from fapws import base evwsgi.start("0.0.0.0", str(port)) evwsgi.set_base_module(base) evwsgi.wsgi_cb(("", wsgifunc)) evwsgi.set_debug(0) evwsgi.run() else: