一个简单的无阻塞tcp套接字服务器实现。
tcpnonblock的Python项目详细描述
TCP非阻塞
一个简单的无阻塞tcp套接字服务器实现。
主要功能
- 使用简单
- 内置线程
- 可扩展-每个连接不使用线程
- 压力测试-可处理100+个连接(在OVH VPS SSD 1上测试)
安装
您可以通过python的pip
模块安装:
使用office python包索引安装:
python3 -m pip install tcpnonblock
或者用这个git呼吸系统
python3 -m pip install git+https://github.com/SamHDev/tcpnonblock.git
注意:如果python3
有效,则使用python
用法
您可以使用以下语句导入库:
importtcpnonblock
服务器示例
下面是一个tcpsocket服务器的快速示例。这是一个Echo Server,用于答复来自客户端的消息。
server=tcpnonblock.TCPSocketServer()# Create a Server Object# Create a Client Instance for the Server@server.client_instanceclassClientInstance(tcpnonblock.TCPSocketServerInstance):# On Client Connectdefconnect(self):print("Client Connected")# On Client Disconnectdefdisconnect(self):print("Client Disconnected")# On Client Messagedefmessage(self,msg):print("Client Message: ",msg)self.send("You Said: ",msg)# Echo the Message (Send)# On Server Start Event@server.on_startdefstart(host,port):print("Server Start")# On Server Stop Event@server.on_stopdefstop():print("Server Stop")# Start the Serverserver.listen("0.0.0.0",8080)# Host,Portserver.start()
客户示例
下面是一个tcpsocket客户机与echo服务器一起使用的快速示例。这是一个与我们的示例交互的Echo Client。
client=remote.TCPSocketClient()# Create a Client Object# On Client Connected to Server@client.on_opendefon_open():print("Connected to Server")client.send("Hello World!")# On Client Disconnected from Server@client.on_closedefon_close():print("Disconnected to Server")# On Server Message Received@client.on_messagedefon_message(msg):print("Reply: ",msg)#`Msg` is a `string`client.connect("localhost",8080)
螺纹
我前面提到的线程呢,很简单。
只需在创建对象时声明threading
参数。
它同时适用于TCPSocketServer
和TCPSocketClient
server=tcpnonblock.TCPSocketServer(threaded=True)client=rtcpnonblock.TCPSocketClient(threaded=True)
线程将在.start()
许可证和属性
由sam huddart在SamHDev别名下为Blume Open Source Project创建。SamHDev/tcpnonblock
在gnu通用公共许可v3.0下获得许可,并且是开源的,如LICENSE所示。商业用途,改性和分销渗透。虽然信用不是必要的,但它是非常有义务的。如果你真的想归功于作者,请将respiratory和作者链接到github或website。感谢您使用我们的工作。