summaryrefslogtreecommitdiff
path: root/docs/socket-activation.rst
blob: 88fd021611fdd15646926235207e758b3a131fc6 (plain)
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
Socket Activation
-----------------

While waitress does not support the various implementations of socket activation,
for example using systemd or launchd, it is prepared to receive pre-bound sockets
from init systems, process and socket managers, or other launchers that can provide
pre-bound sockets.

The following shows a code example starting waitress with two Internet sockets pre-bound sockets.

.. code-block:: python

    import socket
    import waitress


    def app(environ, start_response):
        content_length = environ.get('CONTENT_LENGTH', None)
        if content_length is not None:
            content_length = int(content_length)
        body = environ['wsgi.input'].read(content_length)
        content_length = str(len(body))
        start_response(
            '200 OK',
            [('Content-Length', content_length), ('Content-Type', 'text/plain')]
        )
        return [body]


    if __name__ == '__main__':
        sockets = [
            socket.socket(socket.AF_INET, socket.SOCK_STREAM),
            socket.socket(socket.AF_INET, socket.SOCK_STREAM)]
        sockets[0].bind(('127.0.0.1', 8080))
        sockets[1].bind(('127.0.0.1', 9090))
        waitress.serve(app, sockets=sockets)
        for socket in sockets:
            socket.close()

Generally, to implement socket activation for a given init system, a wrapper
script uses the init system specific libraries to retrieve the sockets from
the init system. Afterwards it starts waitress, passing the sockets with the parameter
``sockets``. Note that the sockets have to be bound, which all init systems
supporting socket activation do.