TCP – TCP handle

class uv.TCP(flags=0, loop=None, on_read=None, on_connection=None)[source]

Stream interface to TCP sockets for clients and servers.

Raises:

uv.UVError – error while initializing the handle

Parameters:
  • flags (int) – tcp flags to be used
  • loop (uv.Loop) – event loop the handle should run on
  • on_read (Callable[[uv.TCP, uv.StatusCodes, bytes], None]:type on_connection: Callable[[uv.TCP, uv.StatusCodes, bytes], None]) – callback which should be called when data has been read
  • on_connection – callback which should run after a new connection has been made or on error (if stream is in listen mode)
open(fd)[source]

Open an existing file descriptor as a tcp handle.

Raises:
  • uv.UVError – error while opening the handle
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

fd (int) – file descriptor

bind(address, flags=0)[source]

Bind the handle to an address. When the port is already taken, you can expect to see an uv.StatusCode.EADDRINUSE error from either bind(), listen() or connect(). That is, a successful call to this function does not guarantee that the call to listen() or connect() will succeed as well.

Raises:
  • uv.UVError – error while binding to address
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:
  • address (uv.Address4 | uv.Address6 | tuple) – address to bind to (ip, port, flowinfo=0, scope_id=0)
  • flags (int) – bind flags to be used (mask of uv.TCPFlags)
connect(address, on_connect=None)[source]

Establish an IPv4 or IPv6 TCP connection.

Raises:
  • uv.UVError – error while connecting to address
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:
  • address (uv.Address4 | uv.Address6 | tuple) – address to connect to
  • on_connect (Callable[[uv.TCPConnectRequest, uv.StatusCode], None]:rtype: uv.TCPConnectRequest) – callback which should run after a connection has been established or on error
sockname

The current address to which the handle is bound to.

Raises:
  • uv.UVError – error while receiving sockname
  • uv.ClosedHandleError – handle has already been closed or is closing
Readonly:

True

Return type:

uv.Address4 | uv.Address6

peername

The address of the peer connected to the handle.

Raises:
  • uv.UVError – error while receiving peername
  • uv.ClosedHandleError – handle has already been closed or is closing
Readonly:

True

Return type:

uv.Address4 | uv.Address6

set_nodelay(enable)[source]

Enable / disable Nagle’s algorithm.

Raises:
  • uv.UVError – error enabling / disabling the algorithm
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

enable (bool) – enable / disable

set_keepalive(enable, delay=0)[source]

Enable / disable TCP keep-alive.

Raises:
  • uv.UVError – error enabling / disabling tcp keep-alive
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:
  • enable (bool) – enable / disable
  • delay (int) – initial delay in seconds
set_simultaneous_accepts(enable)[source]

Enable / disable simultaneous asynchronous accept requests that are queued by the operating system when listening for new TCP connections.

This setting is used to tune a TCP server for the desired performance. Having simultaneous accepts can significantly improve the rate of accepting connections (which is why it is enabled by default) but may lead to uneven load distribution in multi-process setups.

Raises:
  • uv.UVError – error enabling / disabling simultaneous accepts
  • uv.ClosedHandleError – handle has already been closed or is closing
Parameters:

enable (bool) – enable / disable

class uv.TCPFlags[source]

TCP configuration enumeration.

IPV6ONLY = None

Disable dual stack support.

Type:uv.TCPFlags