Pyjo.IOLoop.Server - Non-blocking TCP and UNIX domain socket server

from Pyjo.IOLoop.Server import Server

# Create listen socket
server = Server()
server.on('accept', lambda server, handle: ...)
server.listen(port=3000)

# Or import the module, as in Google Python Style Guide

import Pyjo.IOLoop.Server

# Create listen socket
server = Pyjo.IOLoop.Server.new()
server.on('accept', lambda server, handle: ...)
server.listen(port=3000)

# Start and stop accepting connections
server.start()
server.stop()

# Start reactor if necessary
if not server.reactor.is_running:
    server.reactor.start()

Pyjo.IOLoop.Server accepts TCP/IP and UNIX domain socket connections for Pyjo.IOLoop.

The end of a with block closes the listen socket.

Events

Pyjo.IOLoop.Server inherits all events from Pyjo.EventEmitter and can emit the following new ones.

accept

server.on('accept', lambda server, handle: ...)

Emitted for each accepted connection, with its socket.socket, or the ssl.SSLSocket once TLS has been negotiated.

See also

Pyjoyment, https://mojolicious.org.

Ported from Mojo::IOLoop::Server of Mojolicious 9.49.

class Pyjo.IOLoop.Server.Pyjo_IOLoop_Server[source]

Pyjo.IOLoop.Server inherits all methods from Pyjo.EventEmitter and implements the following new ones.

close() → None[source]
server.close()

Stop accepting connections, close the listen socket and remove it from the PYJO_REUSE environment variable, as it happens when the server is freed.

classmethod generate_port() → int[source]
port = Pyjo.IOLoop.Server.object.generate_port()

Find a free TCP port, primarily used for tests.

property handle: socket | None[source]
handle = server.handle

Get handle for server, usually a socket.socket object.

property is_accepting: bool[source]
boolean = server.is_accepting

Check if connections are currently being accepted.

listen(**options: Any) → None[source]
server.listen(port=3000)

Create a new listen socket.

These options are currently available:

address
address='127.0.0.1'

Local address to listen on, defaults to 0.0.0.0.

backlog
backlog=128

Maximum backlog size, defaults to socket.SOMAXCONN.

fd
fd=3

File descriptor with an already prepared listen socket.

path
path='/tmp/myapp.sock'

Path for UNIX domain socket to listen on.

port
port=80

Port to listen on, defaults to a random port.

reuse
reuse=True

Allow multiple servers to use the same port with the SO_REUSEPORT socket option, which Windows does not have.

single_accept
single_accept=True

Only accept one connection at a time.

tls
tls=True

Enable TLS.

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

Path to TLS certificate authority file.

tls_cert
tls_cert='/etc/tls/server.crt'
tls_cert={'mojolicious.org': '/etc/tls/mojo.crt'}

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

tls_context
tls_context=ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)

The ssl.SSLContext to use instead of the one that the other options make.

tls_key
tls_key='/etc/tls/server.key'
tls_key={'mojolicious.org': '/etc/tls/mojo.key'}

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

property port: int | None[source]
port = server.port

Get port this server is listening on.

property reactor: Pyjo_Reactor[source]
reactor = server.reactor
server.reactor = Pyjo.Reactor.Poll.new()

Low-level event reactor, defaults to the reactor attribute value of the global Pyjo.IOLoop singleton. Note that this attribute is weakened.

start() → None[source]
server.start()

Start or resume accepting connections.

stop() → None[source]
server.stop()

Stop accepting connections.