import socketserver
import threading
#启动服务
class Myhandler(socketserver.BaseRequestHandler):
def setup(self):
super().setup()
self.event = threading.Event()
def handle(self):
super().handle()
print(self.server,self.client_address,self.request)
while not self.event.wait(1):
data = self.request.recv(1024)
msg = 'Your msg = {}'.format(data.decode()).encode()
print(msg)
self.request.send(msg)
def finish(self):
super().finish()
self.event.set()
addr = ('127.0.0.1',9999)
server = socketserver.ThreadingTCPServer(addr,Myhandler)
server.serve_forever()
print('-------------[end]--------------------')
server.shutdown()
server.server_close()
运行结果:
<socketserver.ThreadingTCPServer object at 0x7f1c59c66a58> ('127.0.0.1', 59624) <socket.socket fd=4, family&#