From a0e3cbc5620776569f3c46180a527d6c6143f0f8 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 15 Oct 2011 16:53:23 -0400 Subject: add cherrypy server runner (requires the CherryPy package to be installed --- pyramid/scripts/pserve.py | 105 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 1 + 2 files changed, 106 insertions(+) diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index 53e15c773..a2eded80a 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -843,3 +843,108 @@ def wsgiref_server_runner(wsgi_app, global_conf, **kw): # pragma: no cover server = make_server(host, port, wsgi_app) print('Starting HTTP server on http://%s:%s' % (host, port)) server.serve_forever() + +# For paste.deploy server instantiation (egg:pyramid#cherrypy) +def cherrypy_server_runner( + app, global_conf=None, host='127.0.0.1', port=None, + ssl_pem=None, protocol_version=None, numthreads=None, + server_name=None, max=None, request_queue_size=None, + timeout=None + ): # pragma: no cover + """ + Entry point for CherryPy's WSGI server + + Serves the specified WSGI app via CherryPyWSGIServer. + + ``app`` + + The WSGI 'application callable'; multiple WSGI applications + may be passed as (script_name, callable) pairs. + + ``host`` + + This is the ipaddress to bind to (or a hostname if your + nameserver is properly configured). This defaults to + 127.0.0.1, which is not a public interface. + + ``port`` + + The port to run on, defaults to 8080 for HTTP, or 4443 for + HTTPS. This can be a string or an integer value. + + ``ssl_pem`` + + This an optional SSL certificate file (via OpenSSL) You can + generate a self-signed test PEM certificate file as follows: + + $ openssl genrsa 1024 > host.key + $ chmod 400 host.key + $ openssl req -new -x509 -nodes -sha1 -days 365 \\ + -key host.key > host.cert + $ cat host.cert host.key > host.pem + $ chmod 400 host.pem + + ``protocol_version`` + + The protocol used by the server, by default ``HTTP/1.1``. + + ``numthreads`` + + The number of worker threads to create. + + ``server_name`` + + The string to set for WSGI's SERVER_NAME environ entry. + + ``max`` + + The maximum number of queued requests. (defaults to -1 = no + limit). + + ``request_queue_size`` + + The 'backlog' argument to socket.listen(); specifies the + maximum number of queued connections. + + ``timeout`` + + The timeout in seconds for accepted connections. + """ + is_ssl = False + if ssl_pem: + port = port or 4443 + is_ssl = True + + if not port: + if ':' in host: + host, port = host.split(':', 1) + else: + port = 8080 + bind_addr = (host, int(port)) + + kwargs = {} + for var_name in ('numthreads', 'max', 'request_queue_size', 'timeout'): + var = locals()[var_name] + if var is not None: + kwargs[var_name] = int(var) + + from cherrypy import wsgiserver + + server = wsgiserver.CherryPyWSGIServer(bind_addr, app, + server_name=server_name, **kwargs) + server.ssl_certificate = server.ssl_private_key = ssl_pem + if protocol_version: + server.protocol = protocol_version + + try: + protocol = is_ssl and 'https' or 'http' + if host == '0.0.0.0': + print('serving on 0.0.0.0:%s view at %s://127.0.0.1:%s' % + (port, protocol, port)) + else: + print('serving on %s://%s:%s' % (protocol, host, port)) + server.start() + except (KeyboardInterrupt, SystemExit): + server.stop() + + return server diff --git a/setup.py b/setup.py index 559467fe8..b02459264 100644 --- a/setup.py +++ b/setup.py @@ -99,6 +99,7 @@ setup(name='pyramid', ptweens = pyramid.scripts.ptweens:main [paste.server_runner] wsgiref = pyramid.scripts.pserve:wsgiref_server_runner + cherrypy = pyramid.scripts.pserve:cherrypy_server_runner """ ) -- cgit v1.2.3