Pyjo.Server.Daemon - Non-blocking I/O HTTP and WebSocket server

import Pyjo.Server.Daemon
from Pyjo.Util import b

with Pyjo.Server.Daemon.new(listen=['http://*:8080']) as daemon:
    daemon.unsubscribe('request')

    @daemon.on
    def request(daemon, tx):
        # Request
        method = tx.req.method
        path = tx.req.url.path

        # Response
        tx.res.code = 200
        tx.res.headers.content_type = 'text/plain'
        tx.res.body = b("{0} request for {1}!".format(method, path))

        # Resume transaction
        tx.resume()

    daemon.run()

Pyjo.Server.Daemon is a full featured, highly portable non-blocking I/O HTTP and WebSocket server, with IPv6, TLS, Comet (long polling), keep-alive and multiple event loop support.

Signals

The Pyjo.Server.Daemon process can be controlled at runtime with the following signals.

INT, TERM

Shut down server immediately.

Events

Pyjo.Server.Daemon inherits all events from Pyjo.Server.Base.

Debugging

You can set the PYJO_DAEMON_DEBUG environment variable to get some advanced diagnostics information printed to sys.stderr.

MOJO_DAEMON_DEBUG=1

Classes

class Pyjo.Server.Daemon.Pyjo_Server_Daemon(**kwargs)

Pyjo.Server.Daemon inherits all attributes and methods from Pyjo.Server.Base and implements the following new ones.

acceptors = None
acceptors = daemon.acceptors
daemon.acceptors = []

Active acceptors.

backlog = None
backlog = daemon.backlog
daemon.backlog = 128

Listen backlog size, defaults to SOMAXCONN.

inactivity_timeout = None
timeout = daemon.inactivity_timeout
daemon.inactivity_timeout = 5

Maximum amount of time in seconds a connection can be inactive before getting closed, defaults to the value of the PYJO_INACTIVITY_TIMEOUT environment variable or 15. Setting the value to 0 will allow connections to be inactive indefinitely.

ioloop = None
loop = daemon.ioloop
daemon.ioloop = Pyjo.IOLoop.new()

Event loop object to use for I/O operations, defaults to the global Pyjo.IOLoop singleton.

listen = None
listen = daemon.listen
daemon.listen = ['https://127.0.0.1:8080']

List of one or more locations to listen on, defaults to the value of the PYJO_LISTEN environment variable or http://*:3000 (shortcut for http://0.0.0.0:3000).

# Listen on all IPv4 interfaces
daemon.listen = ['http://*:3000']

# Listen on all IPv4 and IPv6 interfaces
daemon.listen = ['http://[::]:3000']

# Listen on IPv6 interface
daemon.listen = ['http://[::1]:4000']

# Listen on IPv4 and IPv6 interfaces
daemon.listen = ['http://127.0.0.1:3000', 'http://[::1]:3000']

# Allow multiple servers to use the same port (SO_REUSEPORT)
daemon.listen = ['http://*:8080?reuse=1']

# Listen on two ports with HTTP and HTTPS at the same time
daemon.listen = ['http://*:3000', 'https://*:4000']

# Use a custom certificate and key
daemon.listen = ['https://*:3000?cert=/x/server.crt&key=/y/server.key']

# Or even a custom certificate authority
daemon.listen = ['https://*:3000?cert=/x/server.crt&key=/y/server.key&ca=/z/ca.crt']

These parameters are currently available:

ca
ca='/etc/tls/ca.crt'

Path to TLS certificate authority file.

cert
cert='/etc/tls/server.crt'

Path to the TLS cert file, defaults to a built-in test certificate.

ciphers
ciphers='AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH'

Cipher specification string.

key
key='/etc/tls/server.key'

Path to the TLS key file, defaults to a built-in test key.

reuse
reuse=True

Allow multiple servers to use the same port with the SO_REUSEPORT socket option.

verify
verify=0x00

TLS verification mode, defaults to 0x03.

max_clients = None
max_clients = daemon.max_clients
daemon.max_clients = 1000

Maximum number of concurrent connections this server is allowed to handle before stopping to accept new incoming connections, passed along to Pyjo.IOLoop.max_connections.

max_requests = None
max_requests = daemon.max_requests
daemon.max_requests = 100

Maximum number of keep-alive requests per connection, defaults to 25.

run()
daemon.run()

Run server.

silent = None
boolean = daemon.silent
daemon.silent = boolean

Disable console messages.

start()
daemon = daemon.start()

Start accepting connections.

cid  = daemon.set(listen=['http://127.0.0.1']).start().acceptors[0]
port = daemon.ioloop.acceptor(cid).port
stop()
daemon = daemon.stop()

Stop accepting connections. Used by context manager.

Pyjo.Server.Daemon.object

alias of Pyjo_Server_Daemon