examples adapted for unittest structure

This commit is contained in:
Dirk Alders 2025-08-15 20:42:51 +02:00
parent 0aa72da1b8
commit d8683e0606
6 changed files with 48 additions and 72 deletions

View File

@ -44,7 +44,7 @@ except ImportError:
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
__DESCRIPTION__ = """The Module {\\tt %s} is designed to help with client / server tcp socket connections. __DESCRIPTION__ = """The Module {\\tt %s} is designed to help with client / server tcp socket connections.
For more Information read the documentation.""" % __name__.replace('_', '\_') For more Information read the documentation.""" % __name__.replace('_', '\\_')
"""The Module Description""" """The Module Description"""
__INTERPRETER__ = (2, 3) __INTERPRETER__ = (2, 3)
"""The Tested Interpreter-Versions""" """The Tested Interpreter-Versions"""
@ -191,7 +191,8 @@ class tcp_base(object):
if self.__connection__ is None: if self.__connection__ is None:
return None return None
if time.time() > tm + timeout: if time.time() > tm + timeout:
self.logger.warning('%s TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.', self.__log_prefix__(), repr(timeout), repr(num or 'all'), len(self.__receive_buffer__)) self.logger.warning('%s TIMEOUT (%ss): Not enough data in buffer. Requested %s and buffer size is %d.',
self.__log_prefix__(), repr(timeout), repr(num or 'all'), len(self.__receive_buffer__))
return None return None
time.sleep(0.05) time.sleep(0.05)
if num is None: if num is None:
@ -276,10 +277,11 @@ class tcp_server(tcp_base):
**Example:** **Example:**
.. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp_server.py .. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp.py
.. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp_server.log .. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp.log
""" """
def __connect__(self): def __connect__(self):
if self.__socket__ is None: if self.__socket__ is None:
# Create a TCP/IP socket # Create a TCP/IP socket
@ -324,9 +326,9 @@ class tcp_client(tcp_base):
**Example:** **Example:**
.. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp_client.py .. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp.py
.. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp_client.log .. literalinclude:: tcp_socket/_examples_/tcp_socket__tcp.log
""" """
IS_CLIENT = True IS_CLIENT = True
@ -456,9 +458,9 @@ class tcp_server_stp(tcp_server, tcp_base_stp):
**Example:** **Example:**
.. literalinclude:: tcp_socket/_examples_/tcp_socket__stp_server.py .. literalinclude:: tcp_socket/_examples_/tcp_socket__stp.py
.. literalinclude:: tcp_socket/_examples_/tcp_socket__stp_server.log .. literalinclude:: tcp_socket/_examples_/tcp_socket__stp.log
""" """
pass pass
@ -479,8 +481,8 @@ class tcp_client_stp(tcp_client, tcp_base_stp):
**Example:** **Example:**
.. literalinclude:: tcp_socket/_examples_/tcp_socket__stp_client.py .. literalinclude:: tcp_socket/_examples_/tcp_socket__stp.py
.. literalinclude:: tcp_socket/_examples_/tcp_socket__stp_client.log .. literalinclude:: tcp_socket/_examples_/tcp_socket__stp.log
""" """
pass pass

View File

@ -1,21 +1,29 @@
EXAMPLES = $(wildcard tcp*.py) # pylibs examples Makefile: Version 1.0 (2025-08-15)
LOGFILES = ${EXAMPLES:.py=.log}
.ONESHELL:
SHELL = /usr/bin/bash
MAKEFLAGS += --no-print-directory
.SILENT:
INTERPRETER = python3
.PHONY: all .PHONY: all
EXAMPLES := $(wildcard *.py)
EXAMPLES := $(filter-out config.py, $(EXAMPLES))
LOGFILES := ${EXAMPLES:.py=.log}
LOGFILES_1st := ${EXAMPLES:.py=.log_1st}
all: $(LOGFILES) all: $(LOGFILES)
%server.log: %server.py -include __make.d__/*.mk
python3 $< > $@ &
sleep 1 # let the server come up
python3 $(subst server,client,$<) 1> /dev/null
sleep 1 # let the server come down
%client.log: %client.py %.log: %.py
python3 $(subst client,server,$<) 1> /dev/null& $(MAKE) $@_1st 2> /dev/null
sleep 1 # let the server come up echo -e "\e[1m * Running example $<\e[0m"
python3 $< > $@ $(INTERPRETER) $< > $@
sleep 1 # let the server come down
clean: localclean:
@echo "\e[1m * Example logs...\e[0m" clean: localclean
@rm -f $(LOGFILES) echo -e "\e[1m * Example logs...\e[0m"
rm -f $(LOGFILES)
rm -f $(LOGFILES_1st)

View File

@ -1,6 +1,6 @@
import sys # nopep8 import sys # nopep8
sys.path.append('../..') # nopep8 sys.path.append('../..') # nopep8
import time
import tcp_socket import tcp_socket
import report import report
@ -10,15 +10,12 @@ def mirror_callback(tcp):
logger = report.default_logging_config() logger = report.default_logging_config()
# Start the server
s = tcp_socket.tcp_server_stp('127.0.0.1', 17017) s = tcp_socket.tcp_server_stp('127.0.0.1', 17017)
s.register_callback(mirror_callback) s.register_callback(mirror_callback)
i = 0 # Start the client
while not s.is_connected() and i <= 10: c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
i += 1 c.send(b'abc')
time.sleep(.1) # wait for a connection print('The Client received: %s' % repr(c.receive()))
i = 0
while s.is_connected() and i <= 10:
i += 1
time.sleep(.1) # wait for disconnect

View File

@ -1,14 +0,0 @@
import sys # nopep8
sys.path.append('../..') # nopep8
import tcp_socket
import report
def mirror_callback(data):
return data
report.default_logging_config()
c = tcp_socket.tcp_client_stp('127.0.0.1', 17017)
c.send(b'abc')
print('The Client received: %s' % repr(c.receive()))

View File

@ -10,15 +10,12 @@ def mirror_callback(tcp):
report.default_logging_config() report.default_logging_config()
# Start the server
s = tcp_socket.tcp_server('127.0.0.1', 17000) s = tcp_socket.tcp_server('127.0.0.1', 17000)
s.register_callback(mirror_callback) s.register_callback(mirror_callback)
i = 0 # Start the clien
while not s.is_connected() and i <= 10: c = tcp_socket.tcp_client('127.0.0.1', 17000)
i += 1 c.send(b'abc')
time.sleep(.1) # wait for a connection print('The Client received: %s' % repr(c.receive()))
i = 0
while s.is_connected() and i <= 10:
i += 1
time.sleep(.1) # wait for disconnect

View File

@ -1,14 +0,0 @@
import sys # nopep8
sys.path.append('../..') # nopep8
import tcp_socket
import report
def mirror_callback(data):
return data
report.default_logging_config()
c = tcp_socket.tcp_client('127.0.0.1', 17000)
c.send(b'abc')
print('The Client received: %s' % repr(c.receive()))