chunkystreamsocket
- class pymisclib.chunkystreamsocket.ChunkyStreamSocket(host: str = '127.0.0.1', port: int = 10000, sock: ~socket.socket | None = None, logger: ~logging.Logger = <Logger pymisclib.chunkystreamsocket (WARNING)>, debug: bool = False, log_n_bytes: int = 0, _backlog_bytes: bytes | None = None, _chunk_size: int = 4096, _server_socket: bool = False)
Socket used to send message chunks over a TCP connection.
- 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.
- close()
Close the socket.
- connect(timeout: float | None = 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.
InterruptedError – Connection attempt was interrrupted by a signal.
TimeoutError – Connection attempt timed out.
- 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.
- 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.
- property connection: str
Human-readable description of the underlying connection.
- debug: bool = False
True to enable debug output.
- host: str = '127.0.0.1'
Host name or IP address.
- log_n_bytes: int = 0
Number of communicated bytes to log.
- logger: Logger = <Logger pymisclib.chunkystreamsocket (WARNING)>
Logger for diagnostics.
- port: int = 10000
Port number.
- property server_socket: bool
- Is this a server socket (which accepts connections) or a client
socket, which connects to a server?
- Returns:
True if server socket, False if client socket.
- sock: socket | None = None
Socket to use.