tcp_socket (TCP Socket)

Author:

Description:

This Module supports a client/ server tcp socket connection.

Submodules:

Unittest:

See also the unittest 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()))
2023-10-26 00:04:56,969: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer
2023-10-26 00:04:57,025: root.tcp_socket.all_others - INFO - comm-client: Connection established... (to 127.0.0.1:17000)
2023-10-26 00:04:57,025: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer
2023-10-26 00:04:57,070: root.tcp_socket.all_others - INFO - comm-client: TX -> "(3): 61 62 63"
2023-10-26 00:04:57,075: root.tcp_socket.all_others - INFO - comm-client: RX <- "(3): 61 62 63"
2023-10-26 00:04:57,120: 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 and tcp_base_stp. See stringtools.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()))
2023-10-26 00:04:56,616: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer
2023-10-26 00:04:56,768: root.tcp_socket.all_others - INFO - comm-client: Connection established... (to 127.0.0.1:17017)
2023-10-26 00:04:56,768: root.tcp_socket.all_others - DEBUG - comm-client: Cleaning up receive-buffer
2023-10-26 00:04:56,817: root.tcp_socket.all_others - DEBUG - comm-client: TX -> "(7): 3a 3c 61 62 63 3a 3e"
2023-10-26 00:04:56,817: root.tcp_socket.all_others - INFO - comm-client: TX -> "(3): 61 62 63"
2023-10-26 00:04:56,868: root.tcp_socket.all_others - DEBUG - comm-client: RX <- "(7): 3a 3c 61 62 63 3a 3e"
2023-10-26 00:04:56,868: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_IDLE -> STP_STATE_ESCAPE_1
2023-10-26 00:04:56,869: root.stringtools.stp - DEBUG - STP: start pattern (3a 3c) received => changing state STP_STATE_ESCAPE_1 -> STP_STATE_STORE_DATA
2023-10-26 00:04:56,869: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2
2023-10-26 00:04:56,869: root.stringtools.stp - DEBUG - STP: end pattern (3a 3e) received => storing message and changing state STP_STATE_ESCAPE_2 -> STP_STATE_IDLE
2023-10-26 00:04:56,869: root.stringtools.stp - INFO - STP: message identified - (3): 61 62 63
2023-10-26 00:04:56,869: root.tcp_socket.all_others - INFO - comm-client: RX  <- "(3): 61 62 63"
The Client received: b'abc'
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
2023-10-26 00:04:56,364: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer
2023-10-26 00:04:56,364: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17000
2023-10-26 00:04:56,364: root.tcp_socket.all_others - INFO - comm-server: Connection established... (from 127.0.0.1:17000)
2023-10-26 00:04:56,364: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer
2023-10-26 00:04:56,465: root.tcp_socket.all_others - INFO - comm-server: RX <- "(3): 61 62 63"
2023-10-26 00:04:56,465: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer
2023-10-26 00:04:56,465: root.tcp_socket.all_others - INFO - comm-server: TX -> "(3): 61 62 63"
2023-10-26 00:04:56,616: root.tcp_socket.all_others - INFO - comm-server: Connection lost...
2023-10-26 00:04:56,616: 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 and tcp_base_stp. See stringtools.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
2023-10-26 00:04:57,188: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer
2023-10-26 00:04:57,188: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17017
2023-10-26 00:04:57,289: root.tcp_socket.all_others - INFO - comm-server: Connection established... (from 127.0.0.1:17017)
2023-10-26 00:04:57,289: root.tcp_socket.all_others - DEBUG - comm-server: Cleaning up receive-buffer
2023-10-26 00:04:57,390: root.tcp_socket.all_others - DEBUG - comm-server: RX <- "(7): 3a 3c 61 62 63 3a 3e"
2023-10-26 00:04:57,390: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_IDLE -> STP_STATE_ESCAPE_1
2023-10-26 00:04:57,390: root.stringtools.stp - DEBUG - STP: start pattern (3a 3c) received => changing state STP_STATE_ESCAPE_1 -> STP_STATE_STORE_DATA
2023-10-26 00:04:57,390: root.stringtools.stp - DEBUG - STP: data sync (3a) received => changing state STP_STATE_STORE_DATA -> STP_STATE_ESCAPE_2
2023-10-26 00:04:57,390: root.stringtools.stp - DEBUG - STP: end pattern (3a 3e) received => storing message and changing state STP_STATE_ESCAPE_2 -> STP_STATE_IDLE
2023-10-26 00:04:57,390: root.stringtools.stp - INFO - STP: message identified - (3): 61 62 63
2023-10-26 00:04:57,390: root.tcp_socket.all_others - INFO - comm-server: RX  <- "(3): 61 62 63"
2023-10-26 00:04:57,390: root.tcp_socket.all_others - DEBUG - comm-server: TX -> "(7): 3a 3c 61 62 63 3a 3e"
2023-10-26 00:04:57,390: root.tcp_socket.all_others - INFO - comm-server: TX -> "(3): 61 62 63"
2023-10-26 00:04:57,496: root.tcp_socket.all_others - INFO - comm-server: Connection lost...
2023-10-26 00:04:57,496: root.tcp_socket.all_others - INFO - comm-server: Server listening to 127.0.0.1:17017