1 | """ |
---|
2 | Entry point for CherryPy's WSGI server |
---|
3 | """ |
---|
4 | import paste.script.wsgiserver as wsgiserver |
---|
5 | |
---|
6 | def cpwsgi_server(app, global_conf=None, host='127.0.0.1', port=None, |
---|
7 | ssl_pem=None, protocol_version=None, numthreads=None, |
---|
8 | server_name=None, max=None, request_queue_size=None, |
---|
9 | timeout=None): |
---|
10 | """ |
---|
11 | Serves the specified WSGI app via CherryPyWSGIServer. |
---|
12 | |
---|
13 | ``app`` |
---|
14 | |
---|
15 | The WSGI 'application callable'; multiple WSGI applications |
---|
16 | may be passed as (script_name, callable) pairs. |
---|
17 | |
---|
18 | ``host`` |
---|
19 | |
---|
20 | This is the ipaddress to bind to (or a hostname if your |
---|
21 | nameserver is properly configured). This defaults to |
---|
22 | 127.0.0.1, which is not a public interface. |
---|
23 | |
---|
24 | ``port`` |
---|
25 | |
---|
26 | The port to run on, defaults to 8080 for HTTP, or 4443 for |
---|
27 | HTTPS. This can be a string or an integer value. |
---|
28 | |
---|
29 | ``ssl_pem`` |
---|
30 | |
---|
31 | This an optional SSL certificate file (via OpenSSL) You can |
---|
32 | generate a self-signed test PEM certificate file as follows: |
---|
33 | |
---|
34 | $ openssl genrsa 1024 > host.key |
---|
35 | $ chmod 400 host.key |
---|
36 | $ openssl req -new -x509 -nodes -sha1 -days 365 \\ |
---|
37 | -key host.key > host.cert |
---|
38 | $ cat host.cert host.key > host.pem |
---|
39 | $ chmod 400 host.pem |
---|
40 | |
---|
41 | ``protocol_version`` |
---|
42 | |
---|
43 | The protocol used by the server, by default ``HTTP/1.1``. |
---|
44 | |
---|
45 | ``numthreads`` |
---|
46 | |
---|
47 | The number of worker threads to create. |
---|
48 | |
---|
49 | ``server_name`` |
---|
50 | |
---|
51 | The string to set for WSGI's SERVER_NAME environ entry. |
---|
52 | |
---|
53 | ``max`` |
---|
54 | |
---|
55 | The maximum number of queued requests. (defaults to -1 = no |
---|
56 | limit). |
---|
57 | |
---|
58 | ``request_queue_size`` |
---|
59 | |
---|
60 | The 'backlog' argument to socket.listen(); specifies the |
---|
61 | maximum number of queued connections. |
---|
62 | |
---|
63 | ``timeout`` |
---|
64 | |
---|
65 | The timeout in seconds for accepted connections. |
---|
66 | """ |
---|
67 | is_ssl = False |
---|
68 | if ssl_pem: |
---|
69 | port = port or 4443 |
---|
70 | is_ssl = True |
---|
71 | |
---|
72 | if not port: |
---|
73 | if ':' in host: |
---|
74 | host, port = host.split(':', 1) |
---|
75 | else: |
---|
76 | port = 8080 |
---|
77 | bind_addr = (host, int(port)) |
---|
78 | |
---|
79 | kwargs = {} |
---|
80 | for var_name in ('numthreads', 'max', 'request_queue_size', 'timeout'): |
---|
81 | var = locals()[var_name] |
---|
82 | if var is not None: |
---|
83 | kwargs[var_name] = int(var) |
---|
84 | |
---|
85 | server = wsgiserver.CherryPyWSGIServer(bind_addr, app, |
---|
86 | server_name=server_name, **kwargs) |
---|
87 | server.ssl_certificate = server.ssl_private_key = ssl_pem |
---|
88 | if protocol_version: |
---|
89 | server.protocol = protocol_version |
---|
90 | |
---|
91 | try: |
---|
92 | protocol = is_ssl and 'https' or 'http' |
---|
93 | if host == '0.0.0.0': |
---|
94 | print 'serving on 0.0.0.0:%s view at %s://127.0.0.1:%s' % \ |
---|
95 | (port, protocol, port) |
---|
96 | else: |
---|
97 | print "serving on %s://%s:%s" % (protocol, host, port) |
---|
98 | server.start() |
---|
99 | except (KeyboardInterrupt, SystemExit): |
---|
100 | server.stop() |
---|
101 | return server |
---|