server 服务端
import time import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): conn = self.request while True: try: content = conn.recv(1024).decode('utf-8') conn.send(content.upper().encode('utf-8')) time.sleep(0.5) except ConnectionResetError: break server = socketserver.ThreadingTCPServer(('127.0.0.1',9001),Myserver) server.serve_forever()View Code
client客户端
import socket sk = socket.socket() sk.connect(('127.0.0.1',9001)) while True: sk.send(b'hello') content = sk.recv(1024).decode('utf-8') print(content)View Code