Module: anynet.tcp

Provides a TCP client and server.

class TCPClient
A simple TCP client.

async with connect(host: str, port: int) -> TCPClient
Creates a TCP client and connects it to the given address. Blocks until the connection is ready.

async with serve(handler: Callable, host: str = "", port: int = 0) -> None
Creates a TCP server and binds it to the given address. If host is empty, the local address of the default gateway is used. If port is 0, it is chosen by the operating system. handler must be an async function that accepts a TCPClient. The client is closed automatically when handler returns.

TCPClient

async def send(data: bytes) -> None
Sends data through the connection. Blocks if the send buffer is full.

async def recv(num: int = 65536) -> bytes
Receives at most num bytes. Blocks if no data is available.

async def close() -> None
Closes the connection.

def local_address() -> tuple[str, int]
Returns the local address of the client.

def remote_address() -> tuple[str, int]
Returns the remote address of the client.