chunkystreamsocket

class pymisclib.chunkystreamsocket.ChunkyStreamSocket(host: str = '127.0.0.1', port: int = 10000, sock: ~socket.socket = None, logger: ~logging.Logger = <Logger pymisclib.chunkystreamsocket (WARNING)>, debug: bool = False, log_n_bytes: int = 0, _backlog_bytes: bytes = None, _chunk_size: int = 4096)

Socket used to send message chunks over a TCP connection.

__init__(host: str = '127.0.0.1', port: int = 10000, sock: ~socket.socket = None, logger: ~logging.Logger = <Logger pymisclib.chunkystreamsocket (WARNING)>, debug: bool = False, log_n_bytes: int = 0, _backlog_bytes: bytes = None, _chunk_size: int = 4096) None
accept() tuple[int, tuple[str, int]]

Accept a client connection on the (server) socket.

Returns:

The client socket and client address tuple.

Return type:

tuple[int, tuple[str, int]]

Raises:

TimeoutError – No connection accepted withing allowed time.

bind_and_listen(backlog: int = 5, timeout: float = None)

Bind to the host and port to make a server socket.

Parameters:
  • backlog (int) – The maximum number of queued connections.

  • timeout (float) – The number of seconds after which socket operations will time out. Set to None for a blocking socket.

close()

Close the socket.

connect(timeout: float = None)

Connect client socket to the server at host:port.

Parameters:

timeout (float) – The number of seconds after which socket operations will time out. Set to None for a blocking socket.

Raises:

ConnectionError – Failed to establish connection.

recv(length: int, timeout: float = None) bytes

Receive length bytes from the socket.

This function handles chunked data (i.e. the data to receive is split into multiple packets). If more data than expected is received, it is placed into a backlog buffer until the next call to a receive function.

Parameters:
  • length (int) – Number of bytes to read.

  • timeout (float) – Timeout in seconds or None for blocking I/O.

Returns:

The received bytes.

Return type:

bytes

Raises:
  • ConnectionError – The connection was broken.

  • TimeoutError – Timeout while receiving.

recv_to_separator(separator: bytes) bytes

Receive bytes until the given separator is found.

This function handles chunked data (i.e. the data to receive is split into multiple packets). If more data than expected is received, it is placed into a backlog buffer until the next call to a receive function.

Parameters:

separator (bytes) – One or more bytes that separate messages in the TCP stream.

Returns:

The received bytes.

Return type:

bytes

Raises:
  • ConnectionError – The connection was broken.

  • TimeoutError – A timeout occurred while receiving.

send(msg_bytes: bytes) int

Send the bytes.

Parameters:

msg_bytes (bytes) – The bytes to send. If the receiver is expecting a separator, it must be appended to the message by the caller.

Returns:

The number of bytes sent.

Return type:

int

Raises:

ConnectionError – sending failed because the connection was broken.

debug: bool = False
host: str = '127.0.0.1'
log_n_bytes: int = 0
logger: Logger = <Logger pymisclib.chunkystreamsocket (WARNING)>
port: int = 10000
sock: socket = None