cheroot.connections module#

Utilities to manage open connections.

class cheroot.connections.ConnectionManager(server)#

Bases: object

Class which manages HTTPConnection objects.

This is for connections which are being kept-alive for follow-up requests.

_accept_conn(server_socket)#

Accept the connection.

_configure_socket(sock)#

Apply standard settings to a new socket.

_expire(threshold)#

Expire least recently used connections.

Parameters:

threshold (float) – Connections that have not been used within this duration (in seconds), are considered expired and are closed and removed.

This should be called periodically.

_from_server_socket(server_socket)#

Orchestrate the creation of a connection from a server socket.

_is_ignorable_socket_error(exc, /)#

Return True if an OSError during socket operations can be ignored.

property _num_connections#

Return the current number of connections.

Includes all connections registered with the selector, minus one for the server socket, which is always registered with the selector.

_prepare_socket(server_socket)#

Handle physical accept and TLS negotiation.

Returns a 4-tuple of the accepted socket (possibly TLS-wrapped), the remote address and port, a callable that wraps the socket for buffered reading, and a dict of TLS metadata (empty if not using TLS). Raises _NoConnectionAvailable if no connection is ready to accept. Raises ConnectionError if TLS negotiation fails.

_remove_invalid_sockets()#

Clean up the resources of any broken connections.

This method attempts to detect any connections in an invalid state, unregisters them from the selector and closes the file descriptors of the corresponding network sockets where possible.

_run(expiration_interval)#

Run connection handler loop until stop was requested.

Parameters:

expiration_interval (float) – Interval, in seconds, at which connections will be checked for expiration.

Use expiration_interval as select() timeout to assure expired connections are closed in time.

On Windows cap the timeout to 0.05 seconds as select() does not return when a socket is ready.

_send_bad_request_plain_http_error(raw_sock, /)#

Send Bad Request 400 response, and close the socket.

_setup_conn_addr(conn, sock, addr, /)#

Configure remote address and port for the connection.

Populates the connection object with remote address metadata. If the address is unavailable following the handshake, this method detects the socket’s address family (IPv4/IPv6) and provides an appropriate ‘any’ address placeholder.

_wrap_socket_for_tls(raw_socket, addr)#

Handle the TLS wrap and log specific error responses.

On success returns e.g. (SSLSocket, {‘SSL_PROTOCOL’: ‘TLSv1.3’, …}). On failure closes the socket and raises ConnectionError.

property can_add_keepalive_connection#

Flag whether it is allowed to add a new keep-alive connection.

close()#

Close all monitored connections.

put(conn)#

Put idle connection into the ConnectionManager to be managed.

Parameters:

conn (cheroot.server.HTTPConnection) – HTTP connection to be managed

run(expiration_interval)#

Run the connections selector indefinitely.

Args:
expiration_interval (float): Interval, in seconds, at which

connections will be checked for expiration.

Connections that are ready to process are submitted via self.server.process_conn()

Connections submitted for processing must be put() back if they should be examined again for another request.

Can be shut down by calling stop().

stop()#

Stop the selector loop in run() synchronously.

May take up to half a second.

exception cheroot.connections._NoConnectionAvailable#

Bases: Exception

Raised when no connection is ready to be accepted.

class cheroot.connections._ThreadsafeSelector#

Bases: object

Thread-safe wrapper around a DefaultSelector.

There are 2 thread contexts in which it may be accessed:
  • the selector thread

  • one of the worker threads in workers/threadpool.py

The expected read/write patterns are:

Notably, this means _ThreadsafeSelector never needs to worry that connections will be removed behind its back.

The lock is held when iterating or modifying the selector but is not required when select()ing on it.

close()#

Close the selector.

property connections#

Retrieve connections registered with the selector.

register(fileobj, events, data=None)#

Register fileobj with the selector.

select(timeout=None)#

Return socket fd and data pairs from selectors.select call.

Returns entries ready to read in the form:

(socket_file_descriptor, connection)

unregister(fileobj)#

Unregister fileobj from the selector.

cheroot.connections._suppress_socket_io_errors(socket, /)#

Suppress known socket I/O errors.

cheroot.connections.prevent_socket_inheritance(sock)#

Mark the given socket fd as non-inheritable (POSIX).