pyTCP package

Submodules

pyTCP.async_client module

class pyTCP.async_client.AsyncTcpClient(host: str = '127.0.0.1', port: int = 8080, auto_reconnect: bool = True)[source]

Bases: object

Asynchronous tcp client

host

The ip address of the tcp server.

Type:str
port

The port of the tcp server.

Type:int
reader

Instance of the StreamReader

Type:obj:
writer

Instance of the StreamWriter

Type:obj:
auto_reconnect

If true, a reconnect will be made on connection loss.

Type:bool
logger

An instance of the logging module.

Type:
obj:
buffer

The split part of the msg which was not returned.

Type:bool, default=True
close()[source]

Closes the socket connection if it open

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.
is_connected

Returns True if connected.

Type:bool
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:

bytes

Raises:

ClientTimeoutError – Raises if no data was read or no delimiter was found withing the given time.

send(data: bytes)[source]

Send a message to the socket. If an socket.error is raised and auto_connect is enabled, a reconnect will be executed.

Parameters:data (bytes) – Sends the given bytes to the socket.

pyTCP.client module

class pyTCP.client.TcpClient(host: str = '127.0.0.1', port: int = 8080, auto_reconnect: bool = True)[source]

Bases: object

A tcp client

host

The ip address of the tcp server.

Type:str
port

The port of the tcp server.

Type:int
auto_reconnect

If true, a reconnect will be made on connection loss.

Type:bool
logger

An instance of the logging module.

Type:
obj:
buffer

The split part of the msg which was not returned.

Type:bool, default=True
close()[source]

Closes the socket connection if it open

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.
is_connected

Returns True if connected.

Type:bool
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.
send(data: bytes)[source]

Send a message to the socket. If an socket.error is raised and auto_connect is enabled, a reconnect will be executed.

Parameters:data (bytes) – Sends the given bytes to the socket.

pyTCP.client_errors module

exception pyTCP.client_errors.ClientError[source]

Bases: Exception

exception pyTCP.client_errors.ClientProtocolError[source]

Bases: pyTCP.client_errors.ClientError

exception pyTCP.client_errors.ClientSocketError[source]

Bases: pyTCP.client_errors.ClientError

exception pyTCP.client_errors.ClientTimeoutError[source]

Bases: pyTCP.client_errors.ClientError

pyTCP.server module

class pyTCP.server.EchoServer(ip, port, receive_bytes=4096)[source]

Bases: object

last_received

Returns the last received message.

Type:bytes
start_server()[source]

Starts the tcp server.

stop_server()[source]

Stops the tcp server.

class pyTCP.server.ThreadedTCPRequestHandler(request, client_address, server)[source]

Bases: socketserver.BaseRequestHandler

A threaded tcp request handler

handle()[source]

The handle function.

Reads data from the client as long as the server is not requested to close. Sends the read data back to the client and stores it in a queue.

class pyTCP.server.ThreadedTCPServer(server_address, RequestHandlerClass, bind_and_activate=True)[source]

Bases: socketserver.ThreadingMixIn, socketserver.TCPServer

Module contents