TCP — TCP handle

class pyuv.TCP(loop)
Parameters:loop (Loop) – loop object where this handle runs (accessible through TCP.loop).

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

bind((ip, port, [flowinfo, [scope_id]]))
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.

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 128.

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.
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.
  • 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).

writelines(seq[, callback])
Parameters:
  • seq (object) – Data to be written on the TCP connection. It can be any iterable object and the same logic is applied for the contained elements as in the write method.
  • 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).

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.

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.

Previous topic

Timer — Timer handle

Next topic

UDP — UDP handle

This Page