python之路----socketserver模块
socketserver
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socketserver class MyServer(socketserver.BaseRequestHandler): def handle(self): # self.request 就相当于一个conn while True: print(self.client_address) msg = self.request.recv(1024).decode('utf-8') if msg == 'q':break print(msg) info = input('%s>>>'%msg[:2]) self.request.send(info.encode('utf-8')) if __name__ == '__main__': server = socketserver.ThreadingTCPServer(('127.0.0.1',8080),MyServer) # thread 线程 server.allow_reuse_address = True server.serve_forever()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: msg = input('>>>') if msg == 'q': sk.send(b'q') break sk.send(('美团 :'+msg).encode('utf-8')) ret = sk.recv(1024).decode('utf-8') print(ret) sk.close()
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import socket sk = socket.socket() sk.connect(('127.0.0.1',8080)) while True: msg = input('>>>') if msg == 'q': sk.send(b'q') break sk.send(('大众点评 :'+msg).encode('utf-8')) ret = sk.recv(1024).decode('utf-8') print(ret) sk.close()