def run(self, handler): from wsgiserver import CherryPyWSGIServer server = CherryPyWSGIServer((self.host, self.port), handler, numthreads=self.numthreads) server.start()
def WSGIServer(server_address, wsgi_app): """Creates CherryPy WSGI server listening at `server_address` to serve `wsgi_app`. This function can be overwritten to customize the webserver or use a different webserver. """ from wsgiserver import CherryPyWSGIServer return CherryPyWSGIServer(server_address, wsgi_app, server_name="localhost")
def run_cherrypy_server(self, host='localhost', port=9000): from wsgiserver import CherryPyWSGIServer #debug mode can serve static file and check trace app = self if self.debug: app = FileServerMiddleware(app, self.config.get('static', '')) app = ExceptionMiddleware(app) server = CherryPyWSGIServer( (host, port), app)#, server_name='www.cherrypy.example') server.start()
def run(self, handler): from wsgiserver import CherryPyWSGIServer if self.cert and self.key: CherryPyWSGIServer.ssl_certificate = self.cert CherryPyWSGIServer.ssl_private_key = self.key server = CherryPyWSGIServer((self.host, self.port), handler, numthreads=self.connection) server.start()
def runsimple(func, server_address=("0.0.0.0", 8080)): """ Runs [CherryPy][cp] WSGI server hosting WSGI app `func`. The directory `static/` is hosted statically. [cp]: http://www.cherrypy.org """ func = StaticMiddleware(func) func = LogMiddleware(func) from wsgiserver import CherryPyWSGIServer server = CherryPyWSGIServer(server_address, func, server_name="localhost") print "http://%s:%d/" % server_address try: server.start() except KeyboardInterrupt: server.stop()
def runsimple(func, server_address=("0.0.0.0", 8080)): """ Runs [CherryPy][cp] WSGI server hosting WSGI app `func`. The directory `static/` is hosted statically. [cp]: http://www.cherrypy.org """ from wsgiserver import CherryPyWSGIServer from SimpleHTTPServer import SimpleHTTPRequestHandler from BaseHTTPServer import BaseHTTPRequestHandler class StaticApp(SimpleHTTPRequestHandler): """WSGI application for serving static files.""" def __init__(self, environ, start_response): self.headers = [] self.environ = environ self.start_response = start_response def send_response(self, status, msg=""): self.status = str(status) + " " + msg def send_header(self, name, value): self.headers.append((name, value)) def end_headers(self): pass def log_message(*a): pass def __iter__(self): environ = self.environ self.path = environ.get('PATH_INFO', '') self.client_address = environ.get('REMOTE_ADDR','-'), \ environ.get('REMOTE_PORT','-') self.command = environ.get('REQUEST_METHOD', '-') from cStringIO import StringIO self.wfile = StringIO() # for capturing error f = self.send_head() self.start_response(self.status, self.headers) if f: block_size = 16 * 1024 while True: buf = f.read(block_size) if not buf: break yield buf f.close() else: value = self.wfile.getvalue() yield value class WSGIWrapper(BaseHTTPRequestHandler): """WSGI wrapper for logging the status and serving static files.""" def __init__(self, app): self.app = app self.format = '%s - - [%s] "%s %s %s" - %s' def __call__(self, environ, start_response): def xstart_response(status, response_headers, *args): write = start_response(status, response_headers, *args) self.log(status, environ) return write path = environ.get('PATH_INFO', '') if path.startswith('/static/'): return StaticApp(environ, xstart_response) else: return self.app(environ, xstart_response) def log(self, status, environ): outfile = environ.get('wsgi.errors', web.debug) req = environ.get('PATH_INFO', '_') protocol = environ.get('ACTUAL_SERVER_PROTOCOL', '-') method = environ.get('REQUEST_METHOD', '-') host = "%s:%s" % (environ.get( 'REMOTE_ADDR', '-'), environ.get('REMOTE_PORT', '-')) #@@ It is really bad to extend from #@@ BaseHTTPRequestHandler just for this method time = self.log_date_time_string() print >> outfile, self.format % (host, time, protocol, method, req, status) func = WSGIWrapper(func) server = CherryPyWSGIServer(server_address, func, server_name="localhost") print "http://%s:%d/" % server_address try: server.start() except KeyboardInterrupt: server.stop()