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__)
__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"""
__INTERPRETER__ = (2, 3)
"""The Tested Interpreter-Versions"""
@ -191,7 +191,8 @@ class tcp_base(object):
if self.__connection__ is None:
return None
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
time.sleep(0.05)
if num is None:
@ -276,10 +277,11 @@ class tcp_server(tcp_base):
**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):
if self.__socket__ is None:
# Create a TCP/IP socket
@ -324,9 +326,9 @@ class tcp_client(tcp_base):
**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
@ -456,9 +458,9 @@ class tcp_server_stp(tcp_server, tcp_base_stp):
**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
@ -479,8 +481,8 @@ class tcp_client_stp(tcp_client, tcp_base_stp):
**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

View File

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

View File

@ -1,6 +1,6 @@
import sys # nopep8
sys.path.append('../..') # nopep8
import time
import tcp_socket
import report
@ -10,15 +10,12 @@ def mirror_callback(tcp):
logger = report.default_logging_config()
# Start the server
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
# Start the client
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

@ -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()
# Start the server
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
# Start the clien
c = tcp_socket.tcp_client('127.0.0.1', 17000)
c.send(b'abc')
print('The Client received: %s' % repr(c.receive()))

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()))