目的:实现tcp的多连接
""" Server端 1.导入模块 2.创建类,继承 3.多线程 4。永远开启 """ import socketserver class MyService(socketserver.BaseRequestHandler): def handle(self): while 1: info = input('Server>>>') self.request.send(info.encode('utf-8')) if info == 'byte': break res = self.request.recv(1024) new_res = res.decode('utf-8') print(new_res) # if new_res == 'byte': # break if __name__ == '__main__': server = socketserver.ThreadingTCPServer(('127.0.0.1', 8010), MyService) server.serve_forever()
""" Client端 """ import socket sk = socket.socket() sk.connect(('127.0.0.1', 8010)) while 1: msg = sk.recv(1024).decode('utf-8') print(msg) if msg == 'byte': break info = input('Client>>>') sk.send(info.encode('utf-8')) if info == 'byte': break sk.close()