Python——socketsever模块
1、作用:同时与多个客户端通信
import socketserver class MyServer(skcketserver.BaseRequestHandler): def handle(self): while True: msg = self.request.recv(1024).encode('utf-8) if msg == 'q': self.request.close() break print(msg) info = input('>>>') self.request.send(info.encode('utf-8')) if __name__ == '__main__': server = socketserver.ThreadingTcpServer(('127.0.0.1,8080),MyServer) #线程 server.server_forever()
import socketserver sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: msg = input('>>>') if msg == 'q':break sk.send(msg.encode('utf-8') ret = sk.recv(1024).decode('utf-8') print(ret) sk.close()