def __init__(self, ip_port=("0.0.0.0", 80)): self.back_log = 5 # 连接池 self.server = socket(AF_INET, SOCK_STREAM) self.server.bind(ip_port) self.handleMap = {} self.loadMiddleWare() log("开始监听")
def f(conn): start_time = time.time() data = conn.recv(10000) request = Request(data) if request.code == -1: log("failed" + data.decode("utf-8")) conn.close() return response = Response(conn) self.handleRequest(request, response) #将response传给handler进行 log("{0} {1} {2} {3}".format(request.method, request.path, request.args["host"], time.time() - start_time))
def _genCookie(self): if "cookie" in self.args: cookieString = self.args["cookie"] cookies = cookieString.split(";") self.cookie = {} for cookie in cookies: kvs = cookie.split("=") k = kvs[0].strip().lstrip() v = kvs[1].strip().lstrip() self.cookie[k] = v log(self.cookie) else: pass
def listen(self): self.server.listen(self.back_log) while True: conn, addr = self.server.accept() log("接收到来自:" + str(addr) + "的消息:") def f(conn): start_time = time.time() data = conn.recv(10000) request = Request(data) if request.code == -1: log("failed" + data.decode("utf-8")) conn.close() return response = Response(conn) self.handleRequest(request, response) #将response传给handler进行 log("{0} {1} {2} {3}".format(request.method, request.path, request.args["host"], time.time() - start_time)) _thread.start_new_thread(f, (conn, ))
def doHandelList(self, req: Request, res: Response): path = req.path.lstrip("/") log("--------------------- " + path) if path in urls: urls[path](req, res)