python-socketserver实例
import socketserver class MyTCPHandler(socketserver.BaseRequestHandler): def handle(self): while True: try: self.data = self.request.recv(1024).strip() print("{} wrote:".format(self.client_address[0])) print(self.data) self.request.send(self.data.upper()) except ConnectionResetError as e: print("err",e) break if __name__ == "__main__": HOST, PORT = "localhost", 9999 # Create the server, binding to localhost on port 9999 server = socketserver.ThreadingTCPServer((HOST, PORT), MyTCPHandler) server.serve_forever()
客户端实例:
import socket client =socket.socket() client.connect(('localhost',9999)) while True: cmd = input(">>>:").strip() if len(cmd) == 0: continue client.send(cmd.encode("utf-8")) cmd_res = client.recv(1024) print(cmd_res.decode()) client.close()