首页 > 代码库 > python16_day10【SelectWeb、SelectWget、paramiko、pymysql】
python16_day10【SelectWeb、SelectWget、paramiko、pymysql】
一、select实现Web框架(自定义WEB框架)
1 import select 2 import socket 3 4 5 class Flask(object): 6 def __init__(self, routers): 7 self.routers = routers 8 9 def process_data(self, client): 10 data =http://www.mamicode.com/ bytes() 11 while True: 12 try: 13 trunk = client.recv(1024) # 没有数据会报错, 用户断开也会报错. 14 except BlockingIOError as e: 15 trunk = "" 16 if not trunk: 17 break 18 data += trunk 19 data_str = str(data, encoding=‘utf8‘) 20 header, body = data_str.split(‘\r\n\r\n‘, 1) 21 header_list = header.split(‘\r\n‘) 22 header_dict = {} 23 for line in header_list: 24 value = http://www.mamicode.com/line.split(":", 1) 25 if len(value) == 2: 26 k, v = value 27 header_dict[k] = v 28 else: 29 header_dict[‘mothod‘], header_dict[‘url‘], header_dict[‘protocol‘] = line.split(‘ ‘) 30 31 return header_dict, body 32 33 def run(self, host=‘127.0.0.1‘, port=8888): 34 sock = socket.socket() 35 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 36 sock.setblocking(False) 37 sock.bind((host, port)) 38 sock.listen(5) 39 40 inputs = [sock, ] 41 while True: 42 rList, wList, eList = select.select(inputs, [], [], 0.5) 43 for client in rList: 44 # 建立新的连接 45 if client == sock: 46 conn, addr = client.accept() 47 conn.setblocking(False) 48 inputs.append(conn) 49 else: # 用户发送数据 50 header_dict, body = self.process_data(client) 51 request_url = header_dict[‘url‘] 52 func_name = None 53 for item in self.routers: 54 if item[0] == request_url: 55 func_name = item[1] 56 break 57 if not func_name: 58 client.sendall(b"404") 59 else: 60 result = func_name(header_dict, body) 61 client.sendall(result.encode(‘utf8‘)) 62 inputs.remove(client) 63 client.close()
1 import select 2 import socket 3 4 5 class Flask(object): 6 def __init__(self, routers): 7 self.routers = routers 8 9 def process_data(self, client): 10 data =http://www.mamicode.com/ bytes() 11 while True: 12 try: 13 trunk = client.recv(1024) # 没有数据会报错, 用户断开也会报错. 14 except BlockingIOError as e: 15 trunk = "" 16 if not trunk: 17 break 18 data += trunk 19 data_str = str(data, encoding=‘utf8‘) 20 header, body = data_str.split(‘\r\n\r\n‘, 1) 21 header_list = header.split(‘\r\n‘) 22 header_dict = {} 23 for line in header_list: 24 value = http://www.mamicode.com/line.split(":", 1) 25 if len(value) == 2: 26 k, v = value 27 header_dict[k] = v 28 else: 29 header_dict[‘mothod‘], header_dict[‘url‘], header_dict[‘protocol‘] = line.split(‘ ‘) 30 31 return header_dict, body 32 33 def run(self, host=‘127.0.0.1‘, port=8888): 34 sock = socket.socket() 35 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 36 sock.setblocking(False) 37 sock.bind((host, port)) 38 sock.listen(5) 39 40 inputs = [sock, ] 41 while True: 42 rList, wList, eList = select.select(inputs, [], [], 0.5) 43 for client in rList: 44 # 建立新的连接 45 if client == sock: 46 conn, addr = client.accept() 47 conn.setblocking(False) 48 inputs.append(conn) 49 else: # 用户发送数据 50 header_dict, body = self.process_data(client) 51 request_url = header_dict[‘url‘] 52 func_name = None 53 for item in self.routers: 54 if item[0] == request_url: 55 func_name = item[1] 56 break 57 if not func_name: 58 client.sendall(b"404") 59 else: 60 result = func_name(header_dict, body) 61 client.sendall(result.encode(‘utf8‘)) 62 inputs.remove(client) 63 client.close()
python16_day10【SelectWeb、SelectWget、paramiko、pymysql】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。