1. socketserver 多线程用的
例
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import socket import time client=socket.socket() client.connect(("127.0.0.1",9000)) while 1: cmd=input("请输入指令") client.send(cmd.encode("utf-8")) from_server_msg=client.recv(1024).decode("utf-8") print(from_server_msg) client.close()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): while 1: #记住,这边的self.request相当于conn,管道,接收数据 from_client_cmd=self.request.recv(1024) print(from_client_cmd.decode("utf-8")) server_msg=input("服务端说:") self.request.send(server_msg.encode("utf-8")) if __name__=="__main__": # 设置allow_reuse_address允许服务器重用地址 socketserver.TCPServer.allow_reuse_address = True ip_port=("127.0.0.1",9000) # 创建一个server, 将服务地址绑定到"127.0.0.1",9000 server=socketserver.ThreadingTCPServer(ip_port,Myserver) 让服务器永远运行下去,除非强强行停止 server.serve_forever()