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
¶
- handlers_lookup:
- 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 useaddr (
str
) – the address of the clientport (
int
) – the port of the clientssl_ctx (
SSLContext
|None
) – an optional ssl contextoption (
int
|None
) – ormsgpack options can be specified through this parameteruse_pickle (
bool
) – set to True to enable serialization fallback to picklemsg_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 sendmsg_id (
str
|int
|None
) – an optional identifier of the message to sendretry_delay (
int
) – number of seconds to wait before retrying after failuretimeout (
ClientTimeout
) – timeout for the connectionmax_retries (
int
) – maximum number of retries for sending the message (-1 for unbounded retries)
- 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 useport (
int
) – the port to bind to. In case of port forwarding, this is the internal portexternal_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 tossl_ctx (
SSLContext
|None
) – an optional ssl contextget_handler (
Optional
[Callable
[[Request
],Awaitable
[StreamResponse
]]]) – an optional GET handler to usepost_handler (
Optional
[Callable
[[Request
],Awaitable
[StreamResponse
]]]) – an optional POST handler to useoption (
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 requestspost_handler (
Optional
[Callable
[[Request
],Awaitable
[StreamResponse
]]]) – a custom POST handler to handle POST requests
- Return type:
None