Tcp
pyvider.rpcplugin.transport.tcp
¶
pyvider.rpcplugin.transport.tcp¶
TCP Socket Transport implementation using asyncio. Uses Python 3.11+ features such as TypeGuard and structural pattern matching.
Classes¶
TCPSocketTransport
¶
Bases: RPCPluginTransport
TCP socket transport for network-based RPC communication.
This transport implementation provides TCP/IP connectivity for RPC plugins, supporting both server (listen) and client (connect) modes. It's suitable for network communication between processes on the same machine or across a network.
The transport handles: - Dynamic port allocation (when port=0) - DNS resolution for hostnames - Connection lifecycle management - Graceful shutdown with timeouts
Attributes:
| Name | Type | Description |
|---|---|---|
host |
str
|
IP address or hostname to bind/connect to (default: "127.0.0.1") |
port |
int
|
Port number to use (0 for dynamic allocation, default: 0) |
endpoint |
str | None
|
The resolved endpoint string in "host:port" format (set after listen/connect) |
Example
# Server mode with dynamic port
transport = TCPSocketTransport()
endpoint = await transport.listen() # Returns "127.0.0.1:45678"
# Server mode with specific port
transport = TCPSocketTransport(host="0.0.0.0", port=8080)
endpoint = await transport.listen() # Returns "0.0.0.0:8080"
# Client mode
transport = TCPSocketTransport()
await transport.connect("remote.host:8080")
# Cleanup
await transport.close()
Note
This transport is typically created automatically by the factory functions (plugin_server, plugin_client) rather than instantiated directly. For local IPC on Unix-like systems, prefer UnixSocketTransport for better performance and security.
Functions¶
__attrs_post_init__
¶
Initializes locks and events for managing transport state.
Source code in pyvider/rpcplugin/transport/tcp.py
close
async
¶
Closes the TCP transport, including any active server or client connections.
This method is idempotent and ensures that all resources associated with this transport instance are released.
Source code in pyvider/rpcplugin/transport/tcp.py
connect
async
¶
Connects to a remote TCP endpoint.
The endpoint string must be in the format 'host:port'. This method parses the endpoint, performs DNS resolution, and establishes a connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
The target TCP endpoint string (e.g., "127.0.0.1:12345"). |
required |
Raises:
| Type | Description |
|---|---|
TransportError
|
If the endpoint format is invalid, DNS resolution fails, or the connection cannot be established (e.g., timeout, refused). |
Source code in pyvider/rpcplugin/transport/tcp.py
listen
async
¶
endpoint (host:port).
Source code in pyvider/rpcplugin/transport/tcp.py
Functions¶
is_valid_tcp_endpoint
¶
Validate that a TCP endpoint has the correct format.
A valid TCP endpoint must be in the format 'host:port' where: - host is a non-empty string (can be hostname or IP address) - port is a numeric value
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
String to validate as a TCP endpoint. |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[str]
|
TypeGuard[str]: True if the endpoint is valid, False otherwise. |