-
Notifications
You must be signed in to change notification settings - Fork 4
/
web.py
188 lines (142 loc) · 4.38 KB
/
web.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# coding=utf-8
import httpserver
import re
import traceback
class Request(httpserver.HttpRequest):
"""
httppy web 框架的基本请求类
"""
def __init__(self):
httpserver.HttpRequest.__init__(self)
# 存储从url中解析得到的参数
self.url_param = {}
class Response(httpserver.HttpResponse):
"""
httppy web 框架的基本响应类
"""
def __init__(self):
httpserver.HttpResponse.__init__(self)
def redirect(self, redirect_to):
"""
将请求重定向到新的url
:type redirect_to: str
"""
self.set_status(302)
self.set_header('Location', redirect_to)
class Response404(Response):
"""
404响应类
"""
def __init__(self):
Response.__init__(self)
self.set_status(404)
self.handler()
def handler(self):
pass
class Response500(Response):
"""
500响应类
"""
def __init__(self):
Response.__init__(self)
self.set_status(500)
self.handler()
def handler(self):
pass
class WebHandler(httpserver.BaseHttpHandler):
"""
httppy web 框架的handler
"""
def __init__(self, socket_request, client_address, server, url_route):
"""
:type socket_request: socket._socketobject
:type client_address: (str, int)
:type server: WebServer
:type url_route: UrlRoute
"""
self.url_route = url_route
httpserver.BaseHttpHandler.__init__(self, socket_request, client_address, server)
def handle_http_request(self):
if not self.http_request.url.endswith('/'):
self.http_request.url += '/'
return self.url_route.route(self.http_request, self.server)
class WebServer(httpserver.BaseHttpServer):
class _Handler(httpserver.BaseHttpServer._Handler):
def __init__(self, server, url_route):
self.url_route = url_route
httpserver.BaseHttpServer._Handler.__init__(self, server)
def handle_request(self):
self.handler(self.socket_request, self.client_address, self.server, self.url_route)
def __init__(self, server_address, url_route):
"""
:type server_address: (str, int)
:type url_route: UrlRoute
"""
self.url_route = url_route
httpserver.BaseHttpServer.__init__(self, server_address, WebHandler)
def create_thread(self):
return self._Handler(self, self.url_route)
class RequestHandler:
"""
处理UrlRoute分发的请求
"""
def __init__(self, request, server):
"""
:type request: Request
:type server: WebServer
"""
self.request = request
self.server = server
self.logger = self.server.logger
self.response = Response()
# 默认返回内容的类型为html页面
self.response.set_header('Content-Type', "text/html")
self.setup()
self.handle()
self.finish()
def setup(self):
pass
def handle(self):
pass
def finish(self):
pass
def get_response(self):
return self.response
class UrlRoute:
"""
url路由
负责将对应url请求分发给对应handler
"""
response404 = Response404
response500 = Response500
def __init__(self, route_table):
"""
:type route_table: list
"""
self.route_table = route_table
self.convert_route_table()
def convert_route_table(self):
route_table = []
for url, handler in self.route_table:
route_table.append((re.compile(url), handler))
self.route_table = route_table
def route(self, request, server):
"""
:type request: Request
:type server: WebServer
"""
response = None
for url, handler in self.route_table:
result = url.match(request.url)
if result:
request.url_param = result.groupdict()
try:
response = handler(request, server).get_response()
except Exception as e:
# handler出错返回500错误
server.logger.warn('\n'.join([str(e), traceback.format_exc()]))
response = self.response500()
break
if not response:
response = self.response404()
return response