Pyro5.socketutil — Socket related utilities

Low level socket utilities.

class Pyro5.socketutil.SocketConnection(sock: socket, objectId: str = None, keep_open: bool = False)

A wrapper class for plain sockets, containing various methods such as send() and recv()

Pyro5.socketutil.bind_unused_port(sock: socket, host: str | IPv4Address | IPv6Address = 'localhost') int

Bind the socket to a free port and return the port number. This code is based on the code in the stdlib’s test.test_support module.

Pyro5.socketutil.create_bc_socket(bind: Tuple | str = None, reuseaddr: bool = False, timeout: float | None = -1, ipv6: bool = False) socket

Create a udp broadcast socket. Set ipv6=True to create an IPv6 socket rather than IPv4. Set ipv6=None to use the PREFER_IP_VERSION config setting.

Pyro5.socketutil.create_socket(bind: Tuple | str = None, connect: Tuple | str = None, reuseaddr: bool = False, keepalive: bool = True, timeout: float | None = -1, noinherit: bool = False, ipv6: bool = False, nodelay: bool = True, sslContext: SSLContext = None) socket

Create a socket. Default socket options are keepalive and IPv4 family, and nodelay (nagle disabled). If ‘bind’ or ‘connect’ is a string, it is assumed a Unix domain socket is requested. Otherwise, a normal tcp/ip socket tuple (addr, port, …) is used. Set ipv6=True to create an IPv6 socket rather than IPv4. Set ipv6=None to use the PREFER_IP_VERSION config setting.

Pyro5.socketutil.find_probably_unused_port(family: int = AddressFamily.AF_INET, socktype: int = SocketKind.SOCK_STREAM) int

Returns an unused port that should be suitable for binding (likely, but not guaranteed). This code is copied from the stdlib’s test.test_support module.

Pyro5.socketutil.get_interface(ip_address: str | IPv4Address | IPv6Address) IPv4Interface | IPv6Interface

tries to find the network interface that connects to the given host’s address

Pyro5.socketutil.get_ip_address(hostname: str, workaround127: bool = False, version: int = None) IPv4Address | IPv6Address

Returns the IP address for the given host. If you enable the workaround, it will use a little hack if the ip address is found to be the loopback address. The hack tries to discover an externally visible ip address instead (this only works for ipv4 addresses). Set ipVersion=6 to return ipv6 addresses, 4 to return ipv4, 0 to let OS choose the best one or None to use config.PREFER_IP_VERSION.

Pyro5.socketutil.get_ssl_context(servercert: str = '', serverkey: str = '', clientcert: str = '', clientkey: str = '', cacerts: str = '', keypassword: str = '') SSLContext

creates an SSL context and caches it, so you have to set the parameters correctly before doing anything

Pyro5.socketutil.interrupt_socket(address: Tuple[str, int]) None

bit of a hack to trigger a blocking server to get out of the loop, useful at clean shutdowns

Pyro5.socketutil.receive_data(sock: socket, size: int) bytes

Retrieve a given number of bytes from a socket. It is expected the socket is able to supply that number of bytes. If it isn’t, an exception is raised (you will not get a zero length result or a result that is smaller than what you asked for). The partial data that has been received however is stored in the ‘partialData’ attribute of the exception object.

Pyro5.socketutil.send_data(sock: socket, data: bytes) None

Send some data over a socket. Some systems have problems with sendall() when the socket is in non-blocking mode. For instance, Mac OS X seems to be happy to throw EAGAIN errors too often. This function falls back to using a regular send loop if needed.

Pyro5.socketutil.set_keepalive(sock: socket) None

sets the SO_KEEPALIVE option on the socket, if possible.

Pyro5.socketutil.set_nodelay(sock: socket) None

sets the TCP_NODELAY option on the socket (to disable Nagle’s algorithm), if possible.

Pyro5.socketutil.set_noinherit(sock: socket) None

Mark the given socket fd as non-inheritable to child processes

Pyro5.socketutil.set_reuseaddr(sock: socket) None

sets the SO_REUSEADDR option on the socket, if possible.