communication.httphandlers module

This module contains classes for the HTTP server and the HTTP client

class communication.httphandlers.AbstractPool(*args, **kwargs)[source]

Bases: Protocol

Protocol that mimics tno.mpc.communication.Pool.

This protocol contains the minimal Pool interface needed by HTTPClient and HTTPServer. The next major release likely removes the pool parameter entirely in favor of passing the few required attributes directly.

handlers_lookup: dict[str, HTTPClient]
http_server: None | HTTPServer
loop: AbstractEventLoop
class communication.httphandlers.HTTPClient(pool, addr, port, ssl_ctx, option=408, use_pickle=False, msg_prefix=None)[source]

Bases: object

Class that serves as an HTTP Client

__eq__(other)[source]

Equality check for HTTP Clients

Parameters:

other (object) – another HTTP Client

Return type:

bool

Returns:

whether they have the same address and port

__init__(pool, addr, port, ssl_ctx, option=408, use_pickle=False, msg_prefix=None)[source]

Initializes an HTTP client instance

Parameters:
  • pool (AbstractPool) – the communication pool to use

  • addr (str) – the address of the client

  • port (int) – the port of the client

  • ssl_ctx (SSLContext | None) – an optional ssl context

  • option (int | None) – ormsgpack options can be specified through this parameter

  • use_pickle (bool) – set to True to enable serialization fallback to pickle

  • msg_prefix (str | None) – prefix for all sent and received message (e.g. a session id)

Raises:

AttributeError – raised when the provided pool has no assigned http server

recv(msg_id=None)[source]

Request a message from this client

Parameters:

msg_id (str | int | None) – an optional identifier of the message to receive

Return type:

Future[dict[str, Any]]

Returns:

the received message

async send(message, msg_id=None, retry_delay=1, timeout=ClientTimeout(total=300, connect=None, sock_read=None, sock_connect=None, ceil_threshold=5), max_retries=-1)[source]

Sends a POST JSON request to containing the message to this client. If sending of message fails and retry_delay > 0 then retry after retry_delay seconds

Parameters:
  • message (Any) – the message to send

  • msg_id (str | int | None) – an optional identifier of the message to send

  • retry_delay (int) – number of seconds to wait before retrying after failure

  • timeout (ClientTimeout) – timeout for the connection

  • max_retries (int) – maximum number of retries for sending the message (-1 for unbounded retries)

Return type:

None

async shutdown()[source]

Shutdown HTTP Client. Closes open HTTP session.

Return type:

None

class communication.httphandlers.HTTPServer(pool, port, external_port=None, addr='0.0.0.0', ssl_ctx=None, get_handler=None, post_handler=None, option=None, use_pickle=False)[source]

Bases: object

Class for serving an HTTP server

__init__(pool, port, external_port=None, addr='0.0.0.0', ssl_ctx=None, get_handler=None, post_handler=None, option=None, use_pickle=False)[source]

Initalizes an HTTP server instance

Parameters:
  • pool (AbstractPool) – the communication pool to use

  • port (int) – the port to bind to. In case of port forwarding, this is the internal port

  • external_port (int | None) – optional external port that can be set in case of port forwarding. In that case, the external port only serves as identification of this sender to other parties. It should be equal to the port that is visible to other parties (i.e. the port that other parties will send their messages to).

  • addr (str) – the address to bind to

  • ssl_ctx (SSLContext | None) – an optional ssl context

  • get_handler (Optional[Callable[[Request], Awaitable[StreamResponse]]]) – an optional GET handler to use

  • post_handler (Optional[Callable[[Request], Awaitable[StreamResponse]]]) – an optional POST handler to use

  • option (int | None) – ormsgpack options can be specified through this parameter use_pickle: bool = False,

async run_server(get_handler=None, post_handler=None)[source]

Initializes the HTTP server.

Parameters:
  • get_handler (Optional[Callable[[Request], Awaitable[StreamResponse]]]) – a custom GET handler to handle GET requests

  • post_handler (Optional[Callable[[Request], Awaitable[StreamResponse]]]) – a custom POST handler to handle POST requests

Return type:

None

async shutdown()[source]

Shutdown HTTP Server.

Return type:

None