socketpool

The socketpool module provides sockets through a pool. The pools themselves act like CPython’s socket module.

class socketpool.Socket

TCP, UDP and RAW socket. Cannot be created directly. Instead, call SocketPool.socket().

Provides a subset of CPython’s socket.socket API. It only implements the versions of recv that do not allocate bytes objects.

__enter__(self)

No-op used by Context Managers.

__exit__(self)

Automatically closes the Socket when exiting a context. See Lifetime and ContextManagers for more info.

close(self)

Closes this Socket and makes its resources available to its SocketPool.

connect(self, address: tuple)

Connect a socket to a remote address

Parameters:address (~tuple) – tuple of (remote_address, remote_port)
send(self, bytes: ReadableBuffer)

Send some bytes to the connected remote address. Suits sockets of type SOCK_STREAM

Parameters:bytes (~bytes) – some bytes to send
recv_into(self, buffer: WriteableBuffer, bufsize: int)

Reads some bytes from the connected remote address, writing into the provided buffer. If bufsize <= len(buffer) is given, a maximum of bufsize bytes will be read into the buffer. If no valid value is given for bufsize, the default is the length of the given buffer.

Suits sockets of type SOCK_STREAM Returns an int of number of bytes read.

Parameters:
  • buffer (bytearray) – buffer to receive into
  • bufsize (int) – optionally, a maximum number of bytes to read.
settimeout(self, value: int)

Set the timeout value for this socket.

Parameters:value (~int) – timeout in seconds. 0 means non-blocking. None means block indefinitely.
__hash__(self)

Returns a hash for the Socket.

class socketpool.SocketPool

A pool of socket resources available for the given radio. Only one SocketPool can be created for each radio.

SocketPool should be used in place of CPython’s socket which provides a pool of sockets provided by the underlying OS.

socket(self, family: int = AF_INET, type: int = SOCK_STREAM, proto: int = IPPROTO_TCP)

Create a new socket

Parameters:
  • family (~int) – AF_INET or AF_INET6
  • type (~int) – SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
  • proto (~int) – IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW (ignored)
socketpool.getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) → tuple

Gets the address information for a hostname and port

Returns the appropriate family, socket type, socket protocol and address information to call socket.socket() and socket.connect() with, as a tuple.