File: cherrypy_server.py

package info (click to toggle)
pastescript 3.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 784 kB
  • sloc: python: 5,212; sh: 65; makefile: 61
file content (121 lines) | stat: -rw-r--r-- 3,464 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Entry point for CherryPy's WSGI server
"""
try:
    from cherrypy import wsgiserver
except ImportError:
    print('=' * 60)
    print('== You must install CherryPy (pip install cherrypy) to use the egg:PasteScript#cherrypy server')
    print('=' * 60)
    raise

try:
    import ssl
    from cherrypy.wsgiserver.ssl_builtin import BuiltinSSLAdapter
except ImportError:
    builtin = False
else:
    builtin = True

def cpwsgi_server(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):
    """
    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)

    server = wsgiserver.CherryPyWSGIServer(bind_addr, app,
                                           server_name=server_name, **kwargs)
    if is_ssl:
        if builtin:
            server.ssl_module = 'builtin'
            server.ssl_adapter = BuiltinSSLAdapter(ssl_pem, ssl_pem)
        else:
            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