python socket多线程server端

使用socketserver模块实现

import socketserver


class MySockServer(socketserver.BaseRequestHandler):
    def handle(self):
        print("开始一个新的连接,客户端地址:", self.client_address)
        while True:
            data = self.request.recv(1024)
            if not data:
                break
            self.request.send(data)


if __name__ == '__main__':
    host = "127.0.0.1"
    port = 10080
    s = socketserver.ThreadingTCPServer((host, port), MySockServer)
    s.serve_forever()

 

你可能感兴趣的:(Python)