class zokket.tcp.TCPSocket(delegate=None, runloop=None)

Disconnect or stop accepting.

connect(host, port, timeout=None)

Try to establish a connection with host and port.

If a timeout is defined, after this timeout has been reached the TCPSocket will stop trying to connect and the socket_connection_timeout TCPSocketDelegate method will be called.

If the socket establishes the connection before the timeout socket_did_connect TCPSocketDelegate method will be called.

class zokket.tcp.TCPSocketDelegate

An instance of TCPSocket will call methods on its delegate object upon completing certain operations or when it encouters errors.

All instances of TCPSocket should have a delegate that optionally responds to these methods. A delegate should be seen as a connection controller.

socket_accepting(sock, host, port)

This method will be called when a socket is successfully listening on the host and port.

socket_address_in_use(sock, host, port)

This method will be called if the address you tried to accept on is already in use.

socket_address_refused(sock, host, port)

This method will be called if the address you tried to accept on is refused, this usually means you are trying to accept on a port <1024 without root priviledges.

socket_connection_refused(sock, host, port)

The connection was refused

socket_connection_timeout(sock, host, port)

The socket timedout trying to connect to host and port.

socket_did_accept_new_socket(sock, new_sock)

An accept socket accepted a new socket.

socket_did_connect(sock, host, port)

The socket has connected with host and port.

socket_did_disconnect(sock, err=None)

A socket that was previously connected or listening has been closed.

Additionally an error on connecting could cause this delegate method to be called and the exception will be passed.


This method will be called when TLS negotiation is complete, and the rest of the connection will be secure. (Unless sock.stop_tls is called).

socket_read_data(sock, data)

The socket has received data.

socket_wants_runloop_for_new_socket(sock, new_sock)

If this method is not implemented then it will use the current runloop.

This method can be implemented so the socket can run on a seperate thread from the accept socket by returning a different runloop running on a different thread.


The socket calls this method when it is about to connect to a remote socket.

Return True if the socket should continue to connect to the remote socket.