python_socketserver_learning

socketserver 是用来进行多线程的类 

 

  

 1 # author: Roy.G
 2 import socketserver
 3 class myhandleserver(socketserver.BaseRequestHandler):
 4     def handle(self):
 5         while True:
 6             self.data=self.request.recv(1024).strip() #self是server
 7             print('{}来了'.format(self.client_address[0]))
 8             print(self.data)
 9             self.request.send(self.data.upper())
10 
11 if __name__=='__main__': # 智能在运行这个文件时,调用下面的代码,防止被其他代码调用时运行
12     host,port='localhost',9998
13     server = socketserver.ThreadingTCPServer((host,port),myhandleserver)
14     server.serve_forever()

 

posted on 2022-02-05 19:36  ttm6489  阅读(31)  评论(0编辑  收藏  举报

导航