标签:cps elf request alt port code pen 源码 _for
基于tcp,使用socketserver,可以实现一个服务端与多个客户端通信。
完整的socketserver代码示例:
服务端:
import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): while 1: from_client_msg = self.request.recv(1024) print(from_client_msg.decode("utf-8")) server_msg = input("服务端>>>") self.request.send(server_msg.encode("utf-8")) if __name__ == ‘__main__‘: ip_port = ("127.0.0.1", 8001) server = socketserver.ThreadingTCPServer(ip_port, Myserver) server.serve_forever()
客户端:
import socket client = socket.socket() client.connect(("127.0.0.1", 8001)) while 1: client_data = input("客户端>>>") client.send(client_data.encode("utf-8")) from_server_msg = client.recv(1024) print(from_server_msg.decode("utf-8"))
此时即可在一个服务端下,运行多次客户端程序,实现同时与服务端通信。
标签:cps elf request alt port code pen 源码 _for
原文地址:https://www.cnblogs.com/ipython-201806/p/10021749.html