码迷,mamicode.com
首页 > 数据库 > 详细

python16_day10【SelectWeb、SelectWget、paramiko、pymysql】

时间:2017-03-28 21:24:00      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:nbsp   prot   []   turn   split   code   数据   img   protoc   

一、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_day10【SelectWeb、SelectWget、paramiko、pymysql】

标签:nbsp   prot   []   turn   split   code   数据   img   protoc   

原文地址:http://www.cnblogs.com/weibiao/p/6636874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!