transport module
Transport Abstraction Layer
Defines the abstract base classes for network transport, plus in-memory implementations used for deterministic, socket-free testing.
The three ABCs — TransportConnection, TransportServer,
and TransportClient — form the pluggable transport interface.
Real implementations (e.g. network.websocket_transport) and test
implementations (InMemoryServer / InMemoryClient) both
implement these ABCs, allowing protocol and session code to be tested
without real I/O.
- class network.transport.InMemoryClient(server: InMemoryServer)
Bases:
TransportClientIn-memory client that connects to an
InMemoryServer.- Parameters:
server (InMemoryServer) – The server to connect to.
- async connect() TransportConnection
Create a paired connection and register with the server.
- Returns:
The client-side connection endpoint.
- Return type:
- class network.transport.InMemoryServer
Bases:
TransportServerIn-memory server for testing. Clients connect via
InMemoryClient.- async accept(server_conn: TransportConnection)
Called internally when a client connects.
- Parameters:
server_conn (TransportConnection) – The server-side connection endpoint.
- on_connection(handler)
Register an async handler called with each new
TransportConnection.- Parameters:
handler (callable) – Coroutine function accepting a single
TransportConnectionargument.
- async start()
Start listening for connections.
- async stop()
Stop the server and close all active connections.
- class network.transport.InMemoryTransportPair
Bases:
objectCreates a matched pair of connections linked by queues.
Used internally by
InMemoryClientto simulate a network connection within a single process.- static create()
Create two cross-linked
_QueueConnectioninstances.- Returns:
A
(conn_a, conn_b)tuple where sending onconn_adelivers toconn_b.recv()and vice versa.- Return type:
tuple[_QueueConnection, _QueueConnection]
- class network.transport.TransportClient
Bases:
ABCConnects to a server, producing a
TransportConnection.- abstractmethod async connect() TransportConnection
Open a connection to the server.
- Returns:
A connected transport endpoint.
- Return type:
- class network.transport.TransportConnection
Bases:
ABCA single endpoint of a bidirectional connection.
Implementations wrap real sockets or in-memory queues.
- abstractmethod async close() None
Close this connection.
- abstract property closed: bool
Whether this connection has been closed.
- Return type:
bool
- abstractmethod async recv() str
Wait for and return the next message from the remote endpoint.
- Returns:
The received message string.
- Return type:
str
- Raises:
ConnectionError – If the connection is closed or lost.
- abstractmethod async send(data: str) None
Send a message string to the remote endpoint.
- Parameters:
data (str) – The message string to send (typically JSON).
- Raises:
ConnectionError – If the connection is closed.
- class network.transport.TransportServer
Bases:
ABCAccepts incoming connections.
Subclasses must call the registered handler for each new connection.
- abstractmethod on_connection(handler) None
Register an async handler called with each new
TransportConnection.- Parameters:
handler (callable) – Coroutine function accepting a single
TransportConnectionargument.
- abstractmethod async start() None
Start listening for connections.
- abstractmethod async stop() None
Stop the server and close all active connections.