TCP — TCP handle

class pyuv.TCP(loop[, family])
Parameters:
  • loop (Loop) – loop object where this handle runs (accessible through TCP.loop).
  • family (int) – Optionally specify the socket family. If specified and not AF_UNSPEC the socket will be created early.

The TCP handle provides asynchronous TCP functionality both as a client and server.

bind((ip, port, [flowinfo, [scope_id]]), [flags])
Parameters:
  • ip (string) – IP address to bind to.
  • port (int) – Port number to bind to.
  • flowinfo (int) – Flow info, used only for IPv6. Defaults to 0.
  • scope_id (int) – Scope ID, used only for IPv6. Defaults to 0.
  • flags (int) – Binding flags. Only pyuv.UV_TCP_IPV6ONLY is supported at the moment, which disables dual stack support on IPv6 handles.

Bind to the specified IP address and port.

listen(callback[, backlog])
Parameters:
  • callback (callable) – Callback to be called on every new connection. accept() should be called in that callback in order to accept the incoming connection.
  • backlog (int) – Indicates the length of the queue of incoming connections. It defaults to 511.

Start listening for new connections.

Callback signature: callback(tcp_handle, error).

accept(client)
Parameters:client (object) – Client object where to accept the connection.

Accept a new incoming connection which was pending. This function needs to be called in the callback given to the listen() function.

connect((ip, port, [flowinfo, [scope_id]]), callback)
Parameters:
  • ip (string) – IP address to connect to.
  • port (int) – Port number to connect to.
  • flowinfo (int) – Flow info, used only for IPv6. Defaults to 0.
  • scope_id (int) – Scope ID, used only for IPv6. Defaults to 0.
  • callback (callable) – Callback to be called when the connection to the remote endpoint has been made.

Initiate a client connection to the specified IP address and port.

Callback signature: callback(tcp_handle, error).

open(fd)
Parameters:fd (int) – File descriptor to be opened.

Open the given file descriptor (or SOCKET in Windows) as a TCP handle.

..note::
The file descriptor will be closed when the TCP handle is closed, so if it was tasken from a Python socket object, it will be useless afterwards.

Note

Once a file desctiptor has been passed to the open function, the handle ‘owns’ it. When calling close() on the handle, the file descriptor will be closed. If you’d like to keep using it afterwards it’s recommended to duplicate it (using os.dup) before passing it to this function.

Note

The fd won’t be put in non-blocking mode, the user is responsible for doing it.

getsockname()

Return tuple containing IP address and port of the local socket. In case of IPv6 sockets, it also returns the flow info and scope ID (a 4 element tuple).

getpeername()

Return tuple containing IP address and port of the remote endpoint’s socket. In case of IPv6 sockets, it also returns the flow info and scope ID (a 4 element tuple).

shutdown([callback])
Parameters:callback (callable) – Callback to be called after shutdown has been performed.

Shutdown the outgoing (write) direction of the TCP connection.

Callback signature: callback(tcp_handle, error).

write(data[, callback])
Parameters:
  • data (object) – Data to be written on the TCP connection. It can be any Python object conforming to the buffer interface or a sequence of such objects.
  • callback (callable) – Callback to be called after the write operation has been performed.

Write data on the TCP connection.

Callback signature: callback(tcp_handle, error).

try_write(data)
Parameters:data (object) – Data to be written on the TCP connection. It can be any Python object conforming to the buffer interface.

Try to write data on the TCP connection. It will raise an exception (with UV_EAGAIN errno) if data cannot be written immediately or return a number indicating the amount of data written.

start_read(callback)
Parameters:callback (callable) – Callback to be called when data is read from the remote endpoint.

Start reading for incoming data from the remote endpoint.

Callback signature: callback(tcp_handle, data, error).

stop_read()

Stop reading data from the remote endpoint.

nodelay(enable)
Parameters:enable (boolean) – Enable / disable nodelay option.

Enable / disable Nagle’s algorithm.

keepalive(enable, delay)
Parameters:
  • enable (boolean) – Enable / disable keepalive option.
  • delay (int) – Initial delay, in seconds.

Enable / disable TCP keep-alive.

simultaneous_accepts(enable)
Parameters:enable (boolean) – Enable / disable simultaneous accepts.

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.

fileno()

Return the internal file descriptor (or SOCKET in Windows) used by the TCP handle.

Warning

libuv expects you not to modify the file descriptor in any way, and if you do, things will very likely break.

send_buffer_size

Gets / sets the send buffer size.

receive_buffer_size

Gets / sets the receive buffer size.

family

Read only

Returns the socket address family.

write_queue_size

Read only

Returns the size of the write queue.

readable

Read only

Indicates if this handle is readable.

writable

Read only

Indicates if this handle is writable.