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()))
2024-12-26 00:04:41,047:    DEBUG - tcp_socket.all_others - comm-client: Cleaning up receive-buffer
2024-12-26 00:04:41,200:     INFO - tcp_socket.all_others - comm-client: Connection established... (to 127.0.0.1:17000)
2024-12-26 00:04:41,200:    DEBUG - tcp_socket.all_others - comm-client: Cleaning up receive-buffer
2024-12-26 00:04:41,249:     INFO - tcp_socket.all_others - comm-client: TX -> "(3): 61 62 63"
2024-12-26 00:04:41,300:     INFO - tcp_socket.all_others - comm-client: RX <- "(3): 61 62 63"
2024-12-26 00:04:41,350:    DEBUG - tcp_socket.all_others - 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()))
2024-12-26 00:04:39,614:    DEBUG - tcp_socket.all_others - comm-client: Cleaning up receive-buffer
2024-12-26 00:04:39,767:     INFO - tcp_socket.all_others - comm-client: Connection established... (to 127.0.0.1:17017)
2024-12-26 00:04:39,767:    DEBUG - tcp_socket.all_others - comm-client: Cleaning up receive-buffer
2024-12-26 00:04:39,816:    DEBUG - tcp_socket.all_others - comm-client: TX -> "(7): 3a 3c 61 62 63 3a 3e"
2024-12-26 00:04:39,817:     INFO - tcp_socket.all_others - comm-client: TX -> "(3): 61 62 63"
2024-12-26 00:04:39,868:    DEBUG - tcp_socket.all_others - comm-client: RX <- "(7): 3a 3c 61 62 63 3a 3e"
2024-12-26 00:04:39,868:     INFO - tcp_socket.all_others - 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
2024-12-26 00:04:41,421:    DEBUG - tcp_socket.all_others - comm-server: Cleaning up receive-buffer
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
2024-12-26 00:04:39,979:    DEBUG - tcp_socket.all_others - comm-server: Cleaning up receive-buffer