Socket Helpers
Network socket helper module.
Data Structures And Constants
Typed Attributes
- class easynetwork.lowlevel.socket.SocketAttribute
Bases:
TypedAttributeSetTyped attributes which can be used on an endpoint or a transport.
- socket: ISocket = <object object>
socket.socketinstance.
- family: int = <object object>
the socket’s family, as returned by
socket.socket.family.
- sockname: _RetAddress = <object object>
the socket’s own address, result of
socket.socket.getsockname().
- peername: _RetAddress = <object object>
the remote address to which the socket is connected, result of
socket.socket.getpeername().
- class easynetwork.lowlevel.socket.INETSocketAttribute
Bases:
SocketAttributeTyped attributes which can be used on an endpoint or a transport.
- family: Literal[_socket.AddressFamily.AF_INET, _socket.AddressFamily.AF_INET6] = <object object>
the socket’s family, as returned by
socket.socket.family.
- class easynetwork.lowlevel.socket.UNIXSocketAttribute
Bases:
SocketAttributeTyped attributes which can be used on an endpoint or a transport.
Added in version 1.1.
- family: Literal[_socket.AddressFamily.AF_UNIX] = <object object>
the socket’s family, as returned by
socket.socket.family.
- sockname: RawUnixSocketAddress = <object object>
the socket’s own address, result of
socket.socket.getsockname().
- peername: RawUnixSocketAddress = <object object>
the remote address to which the socket is connected, result of
socket.socket.getpeername().
- class easynetwork.lowlevel.socket.TLSAttribute
Bases:
TypedAttributeSetTyped attributes which can be used on an endpoint or a transport.
- sslcontext: SSLContext = <object object>
ssl.SSLContextinstance.
- peercert: _PeerCertRetDictType = <object object>
peer certificate; result of
ssl.SSLSocket.getpeercert().
- cipher: tuple[str, str, int] = <object object>
a three-value tuple containing the name of the cipher being used, the version of the SSL protocol that defines its use, and the number of secret bits being used; result of
ssl.SSLSocket.cipher().
- compression: str = <object object>
the compression algorithm being used as a string, or None if the connection isn’t compressed; result of
ssl.SSLSocket.compression().
Internet addresses
- namedtuple easynetwork.lowlevel.socket.IPv4SocketAddress
Bases:
NamedTupleAn internet (IPv4) socket address.
- namedtuple easynetwork.lowlevel.socket.IPv6SocketAddress
Bases:
NamedTupleAn internet (IPv6) socket address.
- Fields:
- type easynetwork.lowlevel.socket.SocketAddress = IPv4SocketAddress | IPv6SocketAddress
An internet socket address, either IPv4 or IPv6.
Classes
- protocol easynetwork.lowlevel.socket.SupportsSocketOptions
Bases:
ProtocolInterface for an object which support getting and setting socket options.
This protocol is runtime checkable.
Classes that implement this protocol must have the following methods / attributes:
- protocol easynetwork.lowlevel.socket.ISocket
Bases:
SupportsSocketOptions,ProtocolInterface for an object which mirrors a
socket.This protocol is runtime checkable.
Classes that implement this protocol must have the following methods / attributes:
- abstractmethod fileno() int
Similar to
socket.socket.fileno().- Return type:
- abstractmethod get_inheritable() bool
Similar to
socket.socket.get_inheritable().- Return type:
- abstractmethod getpeername() Any
Similar to
socket.socket.getpeername().- Return type:
- abstractmethod getsockname() Any
Similar to
socket.socket.getsockname().- Return type:
- abstract property family: int
Similar to
socket.socket.family.
- abstract property type: int
Similar to
socket.socket.type.
- abstract property proto: int
Similar to
socket.socket.proto.
- final class easynetwork.lowlevel.socket.SocketProxy
Bases:
objectA socket-like wrapper for exposing real transport sockets.
These objects can be safely returned by APIs like client.socket. All potentially disruptive operations (like
socket.socket.close()) are banned.- __init__(socket: ISocket, *, lock: Callable[[], threading.Lock | threading.RLock] | None = None, runner: Callable[[Callable[[], Any]], Any] | None = None) None
- Parameters:
socket (ISocket) – The socket-like object to wrap.
lock (Callable[[], threading.Lock | threading.RLock] | None) – A callback function to use when a lock is required to gain access to the wrapped socket.
runner (Callable[[Callable[[], Any]], Any] | None) – A callback function to use to execute the socket method.
Warning
If lock is ommitted, the proxy object is not thread-safe.
runner can be used for concurrent call management.
Example
Examples of how
ISocket.fileno()would be called according to lock and runner values.Neither lock nor runner:
return socket.fileno()
lock but no runner:
with lock(): return socket.fileno()
runner but no lock:
return runner(socket.fileno)
Both lock and runner:
with lock(): return runner(socket.fileno)
- fileno() int
Calls
ISocket.fileno().- Return type:
- get_inheritable() bool
Calls
ISocket.get_inheritable().- Return type:
- getsockopt(level: int, optname: int, /) int
- getsockopt(level: int, optname: int, buflen: int, /) bytes
Calls
ISocket.getsockopt.
- setsockopt(level: int, optname: int, value: int | bytes, /) None
- setsockopt(level: int, optname: int, value: None, optlen: int, /) None
Calls
ISocket.setsockopt.
- getpeername() Any
Calls
ISocket.getpeername().- Return type:
- getsockname() Any
Calls
ISocket.getsockname().- Return type:
Functions
Internet addresses
- easynetwork.lowlevel.socket.new_socket_address(addr: tuple[str, int], family: Literal[AddressFamily.AF_INET]) IPv4SocketAddress
- easynetwork.lowlevel.socket.new_socket_address(addr: tuple[str, int] | tuple[str, int, int, int], family: Literal[AddressFamily.AF_INET6]) IPv6SocketAddress
- easynetwork.lowlevel.socket.new_socket_address(addr: tuple[Any, ...], family: int) IPv4SocketAddress | IPv6SocketAddress
Factory to create a
SocketAddressfrom addr.Example
>>> import socket >>> new_socket_address(("127.0.0.1", 12345), socket.AF_INET) IPv4SocketAddress(host='127.0.0.1', port=12345) >>> new_socket_address(("::1", 12345), socket.AF_INET6) IPv6SocketAddress(host='::1', port=12345, flowinfo=0, scope_id=0) >>> new_socket_address(("::1", 12345, 12, 345), socket.AF_INET6) IPv6SocketAddress(host='::1', port=12345, flowinfo=12, scope_id=345) >>> new_socket_address(("127.0.0.1", 12345), socket.AF_APPLETALK) Traceback (most recent call last): ... ValueError: Unsupported address family <AddressFamily.AF_APPLETALK: 5>
- Parameters:
addr – The address in the form
(host, port)or(host, port, flow, scope_id).family – The socket family.
- Raises:
ValueError – Invalid family.
TypeError – Invalid addr.
- Returns:
a
SocketAddressnamed tuple.
TCP Options
- easynetwork.lowlevel.socket.set_tcp_nodelay(sock: SupportsSocketOptions, state: bool) None
Enables/Disable Nagle’s algorithm on a TCP socket.
This is equivalent to:
sock.setsockopt(IPPROTO_TCP, TCP_NODELAY, state)
except that if
socket.TCP_NODELAYis not defined, it is silently ignored.- Parameters:
sock (SupportsSocketOptions) – The socket.
Note
Modern operating systems enable it by default.
- easynetwork.lowlevel.socket.set_tcp_keepalive(sock: SupportsSocketOptions, state: bool) None
Enables/Disable keep-alive protocol on a TCP socket.
This is equivalent to:
sock.setsockopt(SOL_SOCKET, SO_KEEPALIVE, state)
except that if
socket.SO_KEEPALIVEis not defined, it is silently ignored.- Parameters:
sock (SupportsSocketOptions) – The socket.
Note
Modern operating systems enable it by default.
Socket Lingering
- namedtuple easynetwork.lowlevel.socket.socket_linger
Bases:
NamedTupleThe socket linger tuple returned by
get_socket_linger().
- easynetwork.lowlevel.socket.get_socket_linger_struct() struct.Struct
Returns a
Structrepresentation of the SO_LINGER structure. See socket(7) for details.The format of the returned struct may vary depending on the operating system.
- Return type:
- easynetwork.lowlevel.socket.get_socket_linger(sock: SupportsSocketOptions) socket_linger
Gets socket linger.
This is equivalent to:
linger_struct = get_socket_linger_struct() (enabled, timeout) = linger_struct.unpack(sock.getsockopt(SOL_SOCKET, SO_LINGER, linger_struct.size))
linger_structis determined by the operating system. Seeget_socket_linger_struct()for details.See the Unix manual page socket(7) for details.
- Parameters:
sock (SupportsSocketOptions) – The socket.
- Return type:
Note
Modern operating systems disable it by default.
- easynetwork.lowlevel.socket.enable_socket_linger(sock: SupportsSocketOptions, timeout: int) None
Enables socket linger.
This is equivalent to:
linger_struct = get_socket_linger_struct() sock.setsockopt(SOL_SOCKET, SO_LINGER, linger_struct.pack(1, timeout))
linger_structis determined by the operating system. Seeget_socket_linger_struct()for details.See the Unix manual page socket(7) for the meaning of the argument timeout.
- Parameters:
sock (SupportsSocketOptions) – The socket.
timeout (int) – How many seconds to linger for.
Note
Modern operating systems disable it by default.
See also
- easynetwork.lowlevel.socket.disable_socket_linger(sock: SupportsSocketOptions) None
Disables socket linger.
This is equivalent to:
linger_struct = get_socket_linger_struct() sock.setsockopt(SOL_SOCKET, SO_LINGER, linger_struct.pack(0, 0))
linger_structis determined by the operating system. Seeget_socket_linger_struct()for details.- Parameters:
sock (SupportsSocketOptions) – The socket.
Note
Modern operating systems disable it by default.
See also