Package: pytcp-message
pytcp_messageView on PyPIRead the docsSends/receives messages in the following format:| 1 byte         | 8 bytes        |  ... |
| is compressed? | content length | data |Messages over 575 bytes are automatically compressed/decompressed with zlibThe main classes areTcpServerandTcpClient, which use the above message format
to communicate with each other.TCP response messages are implemented inTcpMessages.TcpMessagehas one member,content, which is thebytescontent of the message
that will be sent. It also includes convenience methods for writing that content
to and from a TCP socket using the above format.TcpServeropens a connection when a request comes in, then listens on that
connection until either the client closes the connection ortimeoutseconds,
passed to the constructor, have elapsed. The default is 30. This allows multiple
messages to be sent without re-creating new connections.TCP request messages are implemented usingTcpRequests,
which inherit fromTcpMessage.TcpRequestsinclude an additional member,client_addr, the requester's address.When a request comes in,TcpServercreates aTcpRequestobject with the
client's address and content of the incoming message, along with an emptyTcpMessageresponse. These objects are passed to a list of request handlers attached
to the server. After all handlers have run,TcpServersends theTcpMessageresponse object to the client.This allows the server to implement a Chain of Responsibility similar to
Express/NodeJS. By passing objects to the various handlers, requests and
responses can be modified by each method in the chain.Example:importtimefrompytcp_messageimportTcpServer,TcpClientfromdatetimeimportdatetimeADDR="127.0.0.1"PORT=8080deflog_request(req,_):now=datetime.now().strftime("%F%T")print("[{}]{}<--{}".format(now,req.get_client_address()[0],req.get_content().decode("utf-8")))defsend_response(req,res):res.set_content("You are{}:{}".format(*req.get_client_address()).encode("utf-8"))deflog_response(req,res):now=datetime.now().strftime("%F%T")print("[{}]{}-->{}".format(now,res.get_content().decode("utf-8"),req.get_client_address()[0]))defmain():send_message="Hello{}"tcpd=TcpServer(address=ADDR,port=PORT,timeout=5)tcpd.add_request_handler(log_request)tcpd.add_request_handler(send_response)tcpd.add_request_handler(log_response)tcpd.start()print("Server running on tcp://{}:{}...".format(ADDR,PORT))tcpc=TcpClient((ADDR,PORT))foriinrange(0,10):print("Client sending '{}'...".format(send_message.format(i)))tcpc.send(send_message.format(i).encode("utf-8"))response=tcpc.receive()print("Client Received: '{}'\n".format(response.decode("utf-8")))tcpc.stop()print("Client closed connection.")tcpd.stop()time.sleep(5)print("Server stopped.\n")if__name__=="__main__":main()Output:Server running on tcp://127.0.0.1:8080...
Client sending 'Hello0'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello0
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello1'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello1
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello2'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello2
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello3'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello3
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello4'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello4
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello5'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello5
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello6'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello6
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello7'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello7
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello8'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello8
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client sending 'Hello9'...
[2020-06-19 21:01:28] 127.0.0.1 <-- Hello9
[2020-06-19 21:01:28] You are 127.0.0.1:34360 --> 127.0.0.1
Client Received: 'You are 127.0.0.1:34360'

Client closed connection.
Server stopped.