用于TCP消息传递的客户端和服务器
pytcp-message的Python项目详细描述
pytcp_消息
按以下格式发送/接收消息:
| 1 byte | 8 bytes | ... | | is compressed? | content length | data |
超过575字节的消息使用zlib自动压缩/解压缩
{和^是主类} TcpClient,它使用上述消息格式 互相交流。在
TCP响应消息在TcpMessages中实现。
TcpMessage
有一个成员content
,这是消息的bytes
内容
会被发送的。它还包括编写这些内容的方便方法
使用上述格式与TCP套接字进行通信。在
TcpServer
在请求进来时打开一个连接,然后侦听该连接
连接,直到客户端关闭连接或timeout
秒,
传递给构造函数,已过期。默认值为30。这允许多个
在不重新创建新连接的情况下发送的消息。在
TCP请求消息使用TcpRequests实现,
继承自TcpMessage
。TcpRequests
包含一个附加成员,
client_addr
,请求者的地址。在
当一个请求进来时,TcpServer
创建一个TcpRequest
对象
客户端的地址和传入消息的内容,以及空的TcpMessage
回应。这些对象被传递到附加的请求处理程序列表
到服务器。在所有处理程序运行之后,TcpServer
发送TcpMessage
客户端的响应对象。在
这允许服务器实现类似于 快车/NodeJS。通过将对象传递给各种处理程序、请求和 响应可以通过链中的每个方法进行修改。在
示例:
^{pr2}$输出:
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.
- 项目
标签: