python16_day10SelectWebSelectWgetparamikopymysql

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python16_day10SelectWebSelectWgetparamikopymysql相关的知识,希望对你有一定的参考价值。

一、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 = 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 = 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
技术分享
 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 = 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 = 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()
文件2

 

以上是关于python16_day10SelectWebSelectWgetparamikopymysql的主要内容,如果未能解决你的问题,请参考以下文章

python16_day02列表字典集合

python学习day10

python16_day03集合函数递归内置函数

python16_day06类RE模块subprocess模块xml模块shelve模块

python16_day19Django_抽屉项目

python16_day20Django_继续抽屉项目