Welcome to tcp_socket’s documentation!¶
tcp_socket (TCP Socket)¶
Author:
- Dirk Alders <sudo-dirk@mount-mockery.de>
Description:
This Module supports a client/ server tcp socket connection.
Submodules:
Unittest:
See also theunittest
documentation.
Module Documentation:
-
class
tcp_socket.
tcp_base
(host, port, channel_name=None, rx_tx_log_lvl=20)¶ This is the base class for other classes in this module.
Parameters: - host (str) – The host IP for the TCP socket functionality
- port (int) – The port for the TCP socket functionality
- channel_name (str) – The name for the logging channel
Note
This class is not designed for direct usage.
-
client_address
()¶ This method returns the address of the connected client.
Returns: The client address. Return type: str
-
close
()¶ This method closes the connected communication channel, if exists.
-
init_channel_name
(channel_name=None)¶ With this Method, the channel name for logging can be changed.
Parameters: channel_name (str) – The name for the logging channel
-
is_connected
()¶ With this Method the connection status can be identified.
Returns: True, if a connection is established, otherwise False. Return type: bool
-
receive
(timeout=1, num=None)¶ This method returns received data.
Parameters: - timeout (float) – The timeout for receiving data (at least after the timeout the method returns data or None).
- num (int) – the number of bytes to receive (use None to get all available data).
Returns: The received data.
Return type: bytes
-
register_callback
(callback)¶ This method stores the callback which is executed, if data is available. You need to execute
receive()
of this instance given as first argument.Parameters: callback – The callback which will be executed, when data is available.
-
register_connect_callback
(callback)¶ This method stores the callback which is executed, if a connection is established.
Parameters: callback – The callback which will be executed, when a connection is established.
-
register_disconnect_callback
(callback)¶ This method stores the callback which is executed, after the connection is lost.
Parameters: callback – The callback which will be executed, after the connection is lost.
-
send
(data, timeout=1)¶ This method sends data via the initiated communication channel.
Parameters: - data (bytes) – The data to be send over the communication channel.
- timeout (float) – The timeout for sending data (e.g. time to establish new connection).
Returns: True if data had been sent, otherwise False.
Return type: bool
-
class
tcp_socket.
tcp_base_stp
(host, port, channel_name=None)¶ This is the base class for other classes in this module. See also parent
tcp_base
.Parameters: - host (str) – The host IP for the TCP socket functionality
- port (int) – The port for the TCP socket functionality
- channel_name (str) – The name for the logging channel
Note
This class is not designed for direct usage.
-
receive
(timeout=1)¶ This method returns one received messages via the initiated communication channel.
Parameters: timeout (float) – The timeout for receiving data (at least after the timeout the method returns data or None). Returns: The received data. Return type: bytes
-
send
(data, timeout=1)¶ This method sends one stp message via the initiated communication channel.
Parameters: - data (bytes) – The message to be send over the communication channel.
- timeout (float) – The timeout for sending data (e.g. time to establish new connection).
Returns: True if data had been sent, otherwise False.
Return type: bool
-
class
tcp_socket.
tcp_client
(host, port, channel_name=None, rx_tx_log_lvl=20)¶ This class creates a tcp-client for transfering a serial stream of bytes (characters). See also parent
tcp_base
.Parameters: - host (str) – The host IP for the TCP socket functionality
- port (int) – The port for the TCP socket functionality
- channel_name (str) – The name for the logging channel
Note
You need a running
tcp_server
listening at the given IP and Port to be able to communicate.Example:
import sys sys.path.append('../..') import report import tcp_socket import time def mirror_callback(data): return data report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ]) c = tcp_socket.tcp_client('127.0.0.1', 17000) c.send(b'abc') print('The Client received: %s' % repr(c.receive()))
2021-01-11 07:17:54,183: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer 2021-01-11 07:17:54,233: root.tcp_socket.all_others - INFO - comm-client: Connection established... (to 127.0.0.1:17000) 2021-01-11 07:17:54,234: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer 2021-01-11 07:17:54,283: root.tcp_socket.all_others - INFO - comm-client: TX -> "(3): 61 62 63" 2021-01-11 07:17:54,284: root.tcp_socket.all_others - INFO - comm-client: RX <- "(3): 61 62 63" 2021-01-11 07:17:54,334: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer The Client received: b'abc'
-
class
tcp_socket.
tcp_client_stp
(host, port, channel_name=None)¶ This class creates a tcp-client for transfering a message. The bytes will be packed on send and unpacked on receive. See also parents
tcp_client
andtcp_base_stp
. Seestringtools.stp
for more information on packing and unpacking.Parameters: - host (str) – The host IP for the TCP socket functionality
- port (int) – The port for the TCP socket functionality
- channel_name (str) – The name for the logging channel
Note
You need a running
tcp_server_stp
listening at the given IP and Port to be able to communicate.Example:
import sys sys.path.append('../..') import report import tcp_socket import time def mirror_callback(data): return data report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ]) c = tcp_socket.tcp_client_stp('127.0.0.1', 17017) c.send(b'abc') print('The Client received: %s' % repr(c.receive()))
2021-01-11 07:15:57,784: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer 2021-01-11 07:15:57,839: root.tcp_socket.all_others - INFO - comm-client: Connection established... (to 127.0.0.1:17017) 2021-01-11 07:15:57,839: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer
-
class
tcp_socket.
tcp_server
(host, port, channel_name=None, rx_tx_log_lvl=20)¶ This class creates a tcp-server for transfering a serial stream of bytes (characters). See also parent
tcp_base
.Parameters: - host (str) – The host IP for the TCP socket functionality
- port (int) – The port for the TCP socket functionality
- channel_name (str) – The name for the logging channel
Note
You need a
tcp_client
to communicate with the server.Example:
import sys sys.path.append('../..') import report import tcp_socket import time def mirror_callback(tcp): tcp.send(tcp.receive()) report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ]) s = tcp_socket.tcp_server('127.0.0.1', 17000) s.register_callback(mirror_callback) i = 0 while not s.is_connected() and i <= 10: i += 1 time.sleep(.1) # wait for a connection i = 0 while s.is_connected() and i <= 10: i += 1 time.sleep(.1) # wait for disconnect
2021-01-11 07:15:57,392: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer 2021-01-11 07:15:57,392: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17000 2021-01-11 07:15:57,544: root.tcp_socket.all_others - INFO - comm-server: Connection established... (from 127.0.0.1:17000) 2021-01-11 07:15:57,544: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer 2021-01-11 07:15:57,594: root.tcp_socket.all_others - INFO - comm-server: RX <- "(3): 61 62 63" 2021-01-11 07:15:57,595: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer 2021-01-11 07:15:57,595: root.tcp_socket.all_others - INFO - comm-server: TX -> "(3): 61 62 63" 2021-01-11 07:15:57,747: root.tcp_socket.all_others - INFO - comm-server: Connection lost... 2021-01-11 07:15:57,748: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17000
-
class
tcp_socket.
tcp_server_stp
(host, port, channel_name=None)¶ This class creates a tcp-server for transfering a message. The bytes will be packed on send and unpacked on receive. See also parents
tcp_server
andtcp_base_stp
. Seestringtools.stp
for more information on packing and unpacking.Parameters: - host (str) – The host IP for the TCP socket functionality
- port (int) – The port for the TCP socket functionality
- channel_name (str) – The name for the logging channel
Note
You need a
tcp_client_stp
to communicate with the server.Example:
import sys sys.path.append('../..') import report import tcp_socket import time def mirror_callback(tcp): tcp.send(tcp.receive()) report.stdoutLoggingConfigure(log_name_lvl=[('root', 'DEBUG'), ]) s = tcp_socket.tcp_server_stp('127.0.0.1', 17017) s.register_callback(mirror_callback) i = 0 while not s.is_connected() and i <= 10: i += 1 time.sleep(.1) # wait for a connection i = 0 while s.is_connected() and i <= 10: i += 1 time.sleep(.1) # wait for disconnect
2021-01-11 07:17:54,425: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer 2021-01-11 07:17:54,425: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17017 2021-01-11 07:17:54,526: root.tcp_socket.all_others - INFO - comm-server: Connection established... (from 127.0.0.1:17017) 2021-01-11 07:17:54,526: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer 2021-01-11 07:17:54,627: root.tcp_socket.all_others - DEBUG - comm-server: RX <- "(7): 3a 3c 61 62 63 3a 3e" 2021-01-11 07:17:54,627: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_IDLE -> STP_STATE_ESCAPE_1 2021-01-11 07:17:54,628: root.stringtools.stp - DEBUG - STP: start pattern (3a 3c) received => changing state STP_STATE_ESCAPE_1 -> STP_STATE_STORE_DATA 2021-01-11 07:17:54,628: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2 2021-01-11 07:17:54,628: root.stringtools.stp - DEBUG - STP: end pattern (3a 3e) received => storing message and changing state STP_STATE_ESCAPE_2 -> STP_STATE_IDLE 2021-01-11 07:17:54,628: root.stringtools.stp - INFO - STP: message identified - (3): 61 62 63 2021-01-11 07:17:54,628: root.tcp_socket.all_others - INFO - comm-server: RX <- "(3): 61 62 63" 2021-01-11 07:17:54,628: root.tcp_socket.all_others - DEBUG - comm-server: TX -> "(7): 3a 3c 61 62 63 3a 3e" 2021-01-11 07:17:54,629: root.tcp_socket.all_others - INFO - comm-server: TX -> "(3): 61 62 63" 2021-01-11 07:17:54,780: root.tcp_socket.all_others - INFO - comm-server: Connection lost... 2021-01-11 07:17:54,780: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17017