标签:
之前使用的socket模块无法实现服务器并发,通过socketserver模块可以实现服务器并发功能,具体语法如下:
# Author: "libisheng" # Date : 2016/9/29 import socketserver class Myserver(socketserver.BaseRequestHandler): def handle(self): while True: conn = self.request print("服务端启动,正在接受客户端%s:%s的请求..." % self.client_address) print(conn) conn.sendall(bytes("欢迎访问我的网站,请输入要查看的数据!", encoding=‘utf-8‘)) # print(self.client_address) while True: client_data = conn.recv(1024) print(str(client_data, "utf-8")) server_response = input(">>>") conn.sendall(bytes(server_response, encoding="utf-8")) conn.close() if __name__ == ‘__main__‘: server = socketserver.ThreadingTCPServer((‘127.0.0.1‘, 8008), Myserver) server.serve_forever()
标签:
原文地址:http://www.cnblogs.com/libisheng-20160828/p/5919162.html