socketserver
socketserver
-
注意:文件不能与socketserver重名
-
socketserver和server的关系
- socket是底层模块
- socketserver是基于socket完成的
-
作用:处理TCP协议中server端并发的客户请求
-
服务端
# server import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): conn=self.request while True: try: msg=conn.recv(1024).decode('utf-8') conn.send(msg.upper().encode('utf-8')) except ConnectionResetError:break server=socketserver.ThreadingTCPServer(('127.0.0.1',1235),Myserver) server.serve_forever() #永远启动服务端
-
客户端
#client import socket sk=socket.socket() sk.connect(('127.0.0.1',1235)) while True: sk.send('hello'.encode('utf-8')) msg=sk.recv(1024).decode('utf-8') print(msg)