0001"""
0002Entry point for CherryPy's WSGI server
0003"""
0004import paste.script.wsgiserver as wsgiserver
0005
0006def cpwsgi_server(app, global_conf=None, host='127.0.0.1', port=None,
0007 ssl_pem=None, protocol_version=None, numthreads=None,
0008 server_name=None, max=None, request_queue_size=None,
0009 timeout=None):
0010 """
0011 Serves the specified WSGI app via CherryPyWSGIServer.
0012
0013 ``app``
0014
0015 The WSGI 'application callable'; multiple WSGI applications
0016 may be passed as (script_name, callable) pairs.
0017
0018 ``host``
0019
0020 This is the ipaddress to bind to (or a hostname if your
0021 nameserver is properly configured). This defaults to
0022 127.0.0.1, which is not a public interface.
0023
0024 ``port``
0025
0026 The port to run on, defaults to 8080 for HTTP, or 4443 for
0027 HTTPS. This can be a string or an integer value.
0028
0029 ``ssl_pem``
0030
0031 This an optional SSL certificate file (via OpenSSL) You can
0032 generate a self-signed test PEM certificate file as follows:
0033
0034 $ openssl genrsa 1024 > host.key
0035 $ chmod 400 host.key
0036 $ openssl req -new -x509 -nodes -sha1 -days 365 \\
0037 -key host.key > host.cert
0038 $ cat host.cert host.key > host.pem
0039 $ chmod 400 host.pem
0040
0041 ``protocol_version``
0042
0043 The protocol used by the server, by default ``HTTP/1.1``.
0044
0045 ``numthreads``
0046
0047 The number of worker threads to create.
0048
0049 ``server_name``
0050
0051 The string to set for WSGI's SERVER_NAME environ entry.
0052
0053 ``max``
0054
0055 The maximum number of queued requests. (defaults to -1 = no
0056 limit).
0057
0058 ``request_queue_size``
0059
0060 The 'backlog' argument to socket.listen(); specifies the
0061 maximum number of queued connections.
0062
0063 ``timeout``
0064
0065 The timeout in seconds for accepted connections.
0066 """
0067 is_ssl = False
0068 if ssl_pem:
0069 port = port or 4443
0070 is_ssl = True
0071
0072 if not port:
0073 if ':' in host:
0074 host, port = host.split(':', 1)
0075 else:
0076 port = 8080
0077 bind_addr = (host, int(port))
0078
0079 kwargs = {}
0080 for var_name in ('numthreads', 'max', 'request_queue_size', 'timeout'):
0081 var = locals()[var_name]
0082 if var is not None:
0083 kwargs[var_name] = int(var)
0084
0085 server = wsgiserver.CherryPyWSGIServer(bind_addr, app,
0086 server_name=server_name, **kwargs)
0087 server.ssl_certificate = server.ssl_private_key = ssl_pem
0088 if protocol_version:
0089 server.protocol = protocol_version
0090
0091 try:
0092 protocol = is_ssl and 'https' or 'http'
0093 if host == '0.0.0.0':
0094 print 'serving on 0.0.0.0:%s view at %s://127.0.0.1:%s' % (port, protocol, port)
0096 else:
0097 print "serving on %s://%s:%s" % (protocol, host, port)
0098 server.start()
0099 except (KeyboardInterrupt, SystemExit):
0100 server.stop()
0101 return server