communication.communicators.http_communicator module¶
Implementation of the Communicator using HTTP servers.
- class communication.communicators.http_communicator.HttpCommunicator(addr='localhost', port=80, ssl_client_context=None, ssl_server_context=None, conn_timeout=ClientTimeout(total=300, connect=None, sock_read=None, sock_connect=None, ceil_threshold=5), conn_retry_max=3, conn_retry_delay=1)[source]¶
Bases:
Communicator[HttpConnection]The HttpCommunicator implements a Communicator over HTTP. It provides basic capabilities to send and receive bytes to other HttpCommunicators.
The implementation works as follows. Each HttpCommunicator has an HTTP server running to which messages can be sent from other Communicators by sending HTTP POST request to the server.
- __init__(addr='localhost', port=80, ssl_client_context=None, ssl_server_context=None, conn_timeout=ClientTimeout(total=300, connect=None, sock_read=None, sock_connect=None, ceil_threshold=5), conn_retry_max=3, conn_retry_delay=1)[source]¶
Initialises the HttpCommunicator with server information and other specifications.
SSL Configuration One can choose to configure SSL by providing a ssl_client_context and ssl_server_context. Neither or both must be provided.
The ssl_client_context is used by this Communicator to certify itself to an HTTP server (another Communicator) when it sends messages. This should be an SSLContext with purpose SERVER_AUTH. Such a context has the property verify_mode = CERT_REQUIRED by default, meaning that it requires the server to have a (valid) certificate. The context uses our provided CA certificate to validate the server certificate. Additionally, it uses our provided certificate to authenticate us to the server (which may or may not be required by the server).
The ssl_server_context is used by the HTTP server started by this Communicator to certify itself to clients. This SSLContext should have the purpose CLIENT_AUTH. By default, this context does not require client authentication. You probably want to change this to `verify_mode = CERT_REQUIRED`, to ensure mutual TLS.
- Parameters:
addr (
str) – The server host address.port (
int|None) – Optional specification of the server port.ssl_client_context (
SSLContext|None) – The SSLContext to use for HTTP requests made by this Communicator as a client.ssl_server_context (
SSLContext|None) – The SSLContext to use for the HTTP server started by this Communicator.conn_timeout (
ClientTimeout) – Default timeout for client connections. Defaults to 300sconn_retry_max (
int) – Default maximum number of retries for sending a messageconn_retry_delay (
int) – number of seconds to wait before retrying after failure (default is 1s)
- class communication.communicators.http_communicator.HttpConnection(addr, port, cert=None, cert_type=None)[source]¶
Bases:
objectAn HttpConnection represents a single point-to-point connection between us (the local Communicator) and a remote Communicator.
- __init__(addr, port, cert=None, cert_type=None)[source]¶
Initialize a new HttpConnection, representing the connection between us and exactly one other Communicator.
The network_id is automatically determined based on the configuration. The network_id is used to associate traffic with the correct Connection.
The lifecycle of HttpConnection is handled through HttpConnection.setup and HttpConnection.close (call upon creation and deletion).
The HttpConnection exposes a HttpConnection.session which can be used to create HTTP requests for the HTTP server associated with the remote. The session object manages the required HTTP connections.
- Parameters:
addr (
str) – The address of the remote Communicator.port (
int) – The port of the remote Communicator.cert (
Path|str|None) – The path to the public key of the remote Communicator.cert_type (
int|None) – The type of the certificate.
- property network_id: str¶
Returns the network identifier of this Connection.
The network identifier is used to associate HTTP traffic with this Connection.
- Returns:
The network identifier of this Connection.
- property session: ClientSession¶
Returns the aiohttp.ClientSession object associated with this Connection.
The ClientSession manages the HTTP connections created while making requests to the client via the Connection.
- Raises:
AttributeError – if the ClientSession has not yet been created.
- Returns:
The ClientSession associated with this Connection.
- async setup(server_port)[source]¶
Setup the connection.
This method creates a new aiohttp.ClientSession object to be used for sending HTTP requests to the server of the client. The session takes care of efficiently and correctly handling the HTTP connections.
Note: An event_loop must be running when calling this function. This is a requirement imposed by aiohttp.ClientSession. For this reason, this method is asynchronous.
- Parameters:
server_port (
int) – The port of “our” server under which we are known to remotes.- Return type:
None