easy_tcp package¶
Submodules¶
easy_tcp.async_client module¶
-
class
easy_tcp.async_client.
TcpClient
(loop, host: str = '127.0.0.1', port: int = 8080, auto_reconnect: bool = True)[source]¶ Bases:
object
Asynchronous tcp client
-
loop
¶ The asyncio event loop.
Type: obj:
-
reader
¶ Instance of the StreamReader
Type: obj:
-
writer
¶ Instance of the StreamWriter
Type: obj:
-
logger
¶ An instance of the logging module.
Type: obj:
-
connect
(timeout: float = 10.0)[source]¶ Tries to connect to the given host. Waits 0.5 seconds until another try will be made.
Parameters: timeout (float, default 10.0) – The maximum time this function will try to connect until a ClientTimeoutError is raised. Raises: ClientTimeoutError
– If no connection could be established in the given time a ClientTimeoutError is raised.
-
receive
(bytes_to_receive: int = 4096) → bytes[source]¶ Receives messages from the socket. If an socket.error is raised and auto_connect is enabled, a reconnect will be executed, otherwise an empty byte string will be returned.
Parameters: bytes_to_receive (int, default 4096) – Reads the number bytes from the socket. Returns fewer bytes than bytes_to_receive if fewer are available. Returns: The received data from the socket. Or an empty byte string if socket.error is raised. Return type: bytes
-
receive_until
(bytes_to_receive: int = 4096, delimiter: bytes = '\n', timeout: float = 1.0) → bytes[source]¶ Receives messages from the socket until the given delimiter is recognized.
The data will be split at the delimiter. The delimiter will be removed from the message and returned. If the received message contains a message after the delimiter, it will be stored in a buffer and prepended to the next message. If an socket.error is raised and auto_connect is enabled, a reconnect will be executed, otherwise an empty byte string will be returned.
Parameters: - bytes_to_receive (int, default 4096) – Reads the number bytes from the socket. Returns fewer bytes than bytes_to_receive if fewer are available.
- delimiter (bytes, default 'n') – Splits the read data at the delimiter
- timeout (float, default 1.0) – The maximum time this function will wait until a ClientTimeoutError is raised.
Returns: The received data from the socket. Or an empty byte string if socket.error is raised.
Return type: Raises: ClientTimeoutError
– Raises if no data was read or no delimiter was found withing the given time.
-
easy_tcp.client module¶
-
class
easy_tcp.client.
TcpClient
(host: str = '127.0.0.1', port: int = 8080, auto_reconnect: bool = True)[source]¶ Bases:
object
A tcp client
-
logger
¶ An instance of the logging module.
Type: obj:
-
connect
(timeout: float = 10.0)[source]¶ Tries to connect to the given host. Waits 0.5 seconds until another try will be made.
Parameters: timeout (float, default 10.0) – The maximum time this function will try to connect until a ClientTimeoutError is raised. Raises: ClientTimeoutError
– If no connection could be established in the given time a ClientTimeoutError is raised.
-
receive
(bytes_to_receive: int = 4096) → bytes[source]¶ Receives messages from the socket. If an socket.error is raised and auto_connect is enabled, a reconnect will be executed, otherwise an empty byte string will be returned.
Parameters: bytes_to_receive (int, default 4096) – Reads the number bytes from the socket. Returns fewer bytes than bytes_to_receive if fewer are available. Returns: The received data from the socket. Or an empty byte string if socket.error is raised. Return type: bytes
-
receive_until
(bytes_to_receive: int = 4096, delimiter: bytes = '\n', timeout: float = 1.0) → bytes[source]¶ Receives messages from the socket until the given delimiter is recognized.
The data will be split at the delimiter. The delimiter will be removed from the message and returned. If the received message contains a message after the delimiter, it will be stored in a buffer and prepended to the next message. If an socket.error is raised and auto_connect is enabled, a reconnect will be executed, otherwise an empty byte string will be returned.
- bytes_to_receive : int, default 4096
- Reads the number bytes from the socket. Returns fewer bytes than bytes_to_receive if fewer are available.
- delimiter : bytes, default ‘\n’
- Splits the read data at the delimiter
- timeout : float, default 1.0
- The maximum time this function will wait until a ClientTimeoutError is raised.
- bytes
- The received data from the socket. Or an empty byte string if socket.error is raised.
- ClientTimeoutError
- Raises if no data was read or no delimiter was found withing the given time.
-