def start_server(options): """ Start CherryPy server """ global SERVER print 'starting server with options %s' % options if options['daemonize'] == '1' and options['server_user'] and options['server_group']: #ensure the that the daemon runs as specified user change_uid_gid(options['server_user'], options['server_group']) #from cherrypy.wsgiserver import CherryPyWSGIServer as Server from wsgiserver import CherryPyWSGIServer as Server from django.core.handlers.wsgi import WSGIHandler threads = int(options['threads']) SERVER = Server( (options['host'], int(options['port'])), WSGIHandler(), numthreads=threads, max=threads, server_name=options['server_name'], verbose=int(options['verbose']), shutdown_timeout = int(options['shutdown_timeout']), request_queue_size = int(options['request_queue_size']) ) #if options['ssl_certificate'] and options['ssl_private_key']: #server.ssl_certificate = options['ssl_certificate'] #server.ssl_private_key = options['ssl_private_key'] try: SERVER.start() #from django.utils import autoreload #autoreload.main(server.start) except KeyboardInterrupt: SERVER.stop()
def start_server(options): """ Start CherryPy server """ global SERVER try: import newrelic.agent PATH_TO_NEWRELIC = os.path.join(os.getcwd(), 'newrelic.ini') newrelic.agent.initialize(PATH_TO_NEWRELIC) except: print "To run cherrypy instances with newrelic," print "(1) pip install newrelic" print "(2) newrelic-admin generate-config [YOUR_API_KEY] newrelic.ini" print "continuing runcpserver without newrelic..." pass print 'starting server with options %s' % options if options['daemonize'] == '1' and options['server_user'] and options['server_group']: #ensure the that the daemon runs as specified user change_uid_gid(options['server_user'], options['server_group']) #from cherrypy.wsgiserver import CherryPyWSGIServer as Server from wsgiserver import CherryPyWSGIServer as Server from django.core.handlers.wsgi import WSGIHandler threads = int(options['threads']) SERVER = Server( (options['host'], int(options['port'])), WSGIHandler(), numthreads = threads, max = threads, server_name = options['server_name'], verbose = int(options['verbose']), shutdown_timeout = int(options['shutdown_timeout']), request_queue_size = int(options['request_queue_size']) ) #if options['ssl_certificate'] and options['ssl_private_key']: #server.ssl_certificate = options['ssl_certificate'] #server.ssl_private_key = options['ssl_private_key'] try: SERVER.start() #from django.utils import autoreload #autoreload.main(server.start) except KeyboardInterrupt: SERVER.stop()
def start_server(options): """ Start CherryPy server """ global SERVER try: import newrelic.agent PATH_TO_NEWRELIC = os.path.join(os.getcwd(), 'newrelic.ini') newrelic.agent.initialize(PATH_TO_NEWRELIC) except: print "To run cherrypy instances with newrelic," print "(1) pip install newrelic" print "(2) newrelic-admin generate-config [YOUR_API_KEY] newrelic.ini" print "continuing runcpserver without newrelic..." pass print 'starting server with options %s' % options if options['daemonize'] == '1' and options['server_user'] and options[ 'server_group']: #ensure the that the daemon runs as specified user change_uid_gid(options['server_user'], options['server_group']) #from cherrypy.wsgiserver import CherryPyWSGIServer as Server from wsgiserver import CherryPyWSGIServer as Server from django.core.handlers.wsgi import WSGIHandler threads = int(options['threads']) SERVER = Server((options['host'], int(options['port'])), WSGIHandler(), numthreads=threads, max=threads, server_name=options['server_name'], verbose=int(options['verbose']), shutdown_timeout=int(options['shutdown_timeout']), request_queue_size=int(options['request_queue_size'])) #if options['ssl_certificate'] and options['ssl_private_key']: #server.ssl_certificate = options['ssl_certificate'] #server.ssl_private_key = options['ssl_private_key'] try: SERVER.start() #from django.utils import autoreload #autoreload.main(server.start) except KeyboardInterrupt: SERVER.stop()
def run(self): """Launch CherryPy Django web server.""" options = CPSERVER_OPTIONS server = CherryPyWSGIServer( (options['host'], int(options['port'])), WSGIPathInfoDispatcher({ '/': WSGIHandler(), urlparse(settings.MEDIA_URL).path: MediaHandler( settings.MEDIA_ROOT), settings.ADMIN_MEDIA_PREFIX: MediaHandler( os.path.join(admin.__path__[0], 'media')) }), int(options['threads']), options['host'], request_queue_size=int(options['request_queue_size'])) 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 """ 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 start(self): """ Starts the WSGI server. """ server = CherryPyWSGIServer((self.host, self.port), self.dispatcher) server.environ.update(self.wsgi_env) if self.daemonize: daemonize() if self.pidfile: print "writing pid (%s)" % self.pidfile writepid(self.pidfile) # setup the process environment for var, value in self.env.items(): os.environ[var] = value try: server.start() except KeyboardInterrupt: # likely not a daemon so make sure to shutdown properly. server.stop()
class WebServer(object): http_date = "%a, %d %b %Y %H:%M:%S %Z" http_date_gmt = "%a, %d %b %Y %H:%M:%S GMT" def __init__(self, host, port): self.host = host self.port = port self.server = CherryPyWSGIServer \ ((self.host, self.port), self.service_request) # XXX self.server.environ["wsgi.version"] = (1, 1) def start(self): self.server.start() def stop(self): self.server.stop() def service_request(self, env, response): pass def send_not_found(self, response, headers): message = "404 Not Found" headers.append(("Content-Length", str(len(message)))) headers.append(("Content-Type", "text/plain")) response(message, headers) return (message,) def send_message(self, response, headers, message): headers.append(("Content-Length", str(len(message)))) headers.append(("Content-Type", "text/plain")) response(message, headers) return (message,) def send_redirect(self, response, headers, url): headers.append(("Location", url)) headers.append(("Content-Length", "0")) response("303 See Other", headers) return () def send_not_modified(self, response, headers): headers.append(("Content-Length", "0")) response("304 Not Modified", headers) return () def print_url_vars(self, query, writer): writer.write("URL variables:\n\n") if query: vars = query.split(";") for var in sorted(vars): key, value = var.split("=") writer.write(" %-30s %s\n" % (key, value)) writer.write("\n") def print_environment(self, env, writer): writer.write("Environment:\n\n") for key in sorted(env): value = env[key] writer.write(" %-30s %s\n" % (key, value)) writer.write("\n")
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()
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() msg = self.format % (host, time, protocol, method, req, status) print >> outfile, utils.safestr(msg) func = WSGIWrapper(func) server = CherryPyWSGIServer(server_address, func, server_name="localhost") print "http://%s:%d/" % server_address try: server.start() except KeyboardInterrupt: server.stop()
#itemlist.extend(local_itemlist) itemlist.append(web.form.Button('Submit')) my_form = web.form.Form(*itemlist) urls = ('/', 'D4Web') render = web.template.render('templates/') app = web.application(urls, globals()) func = app.wsgifunc() server_address = validip('127.0.0.1',8080) func = StaticMiddleware(func) #func = LogMiddleware(func) server = CherryPyWSGIServer(server_address, func, server_name="localhost",numthreads=1) #print "http://%s:%d/" % server_address class D4Web: def GET(self): form = my_form() return render.D4Web( form ) form.validates() s = str(form.value) stderr_return(s) return s try: server.start() except (KeyboardInterrupt, SystemExit): server.stop() server = None