1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#-*- coding: utf-8 -*- import socketserver import socket """ 测试环境:Ubuntu Pyhton3.6.5 web访问: http://127.0.0.1:9999 通过 curl: curl http://127.0.0.1:9999 通过 """ class myHandler(socketserver.StreamRequestHandler): def handle(self): self.rdata = self.rfile.readline() # 测试for方法独到最后会持续阻塞 # for item in self.rfile: # print("{} write: {}".format(self.client_address[0], item)) self.function1() self.wfile.write(b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<html>hello</html>') def function1(self): if 'GET' in self.rdata.decode('utf-8'): self.function2() def function2(self): """测试rfile中的数据可以缓存后续继续read""" while True: item = self.rfile.readline() # 此处无法设置非阻塞,通过判断http请求中最后一组数据为\r\n来退出循环 if item == b"\r\n": break print("{} write: {}".format(self.client_address[0], item)) if __name__ == "__main__": # 全局设置端口重用 socketserver.ThreadingTCPServer.allow_reuse_address = True server = socketserver.ThreadingTCPServer(('127.0.0.1', 9999), myHandler) # 设置端口重用 # server.allow_reuse_address = True # 网上关于设置socket参数方法 # server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) # 设置非阻塞不生效 server.socket.setblocking(False) server.serve_forever() |
python2: SockServer
python3: socketserver
如果pyhton2一直提示找不到模块,尝试 pip install Werkzeug --upgrade