libp2p.transport.tcp package
Submodules
libp2p.transport.tcp.tcp module
- class libp2p.transport.tcp.tcp.TCP(*, dns_resolution_timeout: float = 5.0, dns_max_retries: int = 3)
Bases:
ITransport- can_dial(maddr: Multiaddr) bool
Return True if this TCP transport can dial the given multiaddr.
Accepts pure TCP addresses (/ip4/…/tcp/… or /ip6/…/tcp/…) but rejects WebSocket addresses (/ws, /wss) even though they use TCP underneath, so the TransportManager routes those to WebsocketTransport instead.
- Parameters:
maddr – The multiaddress to check.
- Returns:
True if this transport handles the multiaddr.
- can_listen(maddr: Multiaddr) bool
Return True if this TCP transport can listen on the given multiaddr.
- Parameters:
maddr – The multiaddress to check.
- Returns:
True if this transport can listen on the multiaddr.
- create_listener(handler_function: Callable[[ReadWriteCloser], Awaitable[None]]) TCPListener
Create listener on transport.
- Parameters:
handler_function – a function called when a new connection is received that takes a connection as argument which implements interface-connection
- Returns:
a listener object that implements listener_interface.py
- async dial(maddr: Multiaddr) IRawConnection
Dial a transport to peer listening on multiaddr.
Resolves DNS (dns, dns4, dns6, dnsaddr) before dialing (Phase 3.1).
- Parameters:
maddr – multiaddr of peer
- Returns:
RawConnection if successful
- Raises:
OpenConnectionError – raised when failed to open connection
- class libp2p.transport.tcp.tcp.TCPListener(handler_function: Callable[[ReadWriteCloser], Awaitable[None]])
Bases:
IListener- async close() None
Cancel the listener’s internal nursery and close all sockets.
Safe to call multiple times. Once closed, further
listen()calls raiseOpenConnectionError. Waits for the background system task to finish before returning.
- get_addrs() tuple[Multiaddr, ...]
Retrieve list of addresses the listener is listening on.
- Returns:
return list of addrs
- async listen(maddr: Multiaddr) None
Put listener in listening mode and wait for incoming connections.
On the first call, the listener spawns its own internal nursery as a trio system task so that
serve_tcpkeeps running afterlisten()returns. Subsequent calls add additional binds to the same nursery. The nursery is cancelled onclose().- Parameters:
maddr – maddr of peer
- Raises:
OpenConnectionError – if listening fails (e.g. missing/invalid port, failed start, or listener already closed)