-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.py
214 lines (188 loc) · 8.2 KB
/
simple.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# -*- coding: utf-8 -*-
from wsgiref.headers import Headers
from base64 import decodestring
class Application:
def __init__(self):
self.routes = {
'/': self.root,
'/debug': self.debug,
'/favicon.ico': self.favicon,
'/error': self.error
}
def __call__(self, environ, start_response):
method = environ.get('REQUEST_METHOD')
uri = environ.get('PATH_INFO')
if method not in ('HEAD', 'GET', 'POST'):
return self.method_not_allowed(environ, start_response)
if uri not in self.routes:
return self.page_not_found(environ, start_response)
return self.routes[uri](environ, start_response)
def get_headers(self, environ):
tmp = []
for key, val in environ.items():
if key[:5] == 'HTTP_':
key = '-'.join(map(lambda x: x.capitalize(),
key[5:].split('_')))
tmp.append((key, val))
return Headers(tmp)
def get_crumbnav(self, environ):
navs = [environ.get('HTTP_HOST')]
uri = environ.get('PATH_INFO')
if uri == '/':
navs.append('<b>/</b>')
else:
navs.append('<a href="/">/</a>')
navs.append('<b>%s</b>' % uri)
return " » ".join(navs)
def root(self, environ, start_response):
buff = [
"<html>",
"<head>",
'<meta http-equiv="content-type" content="text/html;'
' charset=utf-8"/>',
"<title>Simple WSGI Example Application</title>",
"</head>",
"<body>",
"<h1>Simple WSGI Example Application</h1>",
self.get_crumbnav(environ),
"<ul>",
'<li><a href="/">/</a> - This Page</li>',
'<li><a href="/debug">/debug</a> - Debug Page</li>',
'<li><a href="/no-page">/no-page</a> - No Exist Page</li>',
"</ul>",
"<hr>",
"<small>Copyright (c) 2013 Ondřej Tůma. See ",
'<a href="http://poorhttp.zeropage.cz">poorhttp.zeropage.cz</a>'
'</small>.',
"</body>",
"</html>"
]
response_headers = [('Content-type', 'text/html'),
('X-Application', 'Simple')]
start_response("200 OK", response_headers)
return [line.encode('utf-8') + b'\n' for line in buff]
def debug(self, environ, start_response):
headers = self.get_headers(environ)
buff = ([
"<html>",
"<head>",
'<meta http-equiv="content-type" content="text/html;'
' charset=utf-8"/>',
"<title>Debug Page</title>",
"<style>",
"table { width: 100%; font-family: monospace; }",
"td { word-break:break-word; }",
"td:first-child { white-space: nowrap; word-break:keep-all; }",
"tr:hover { background: #e0e0e0; }",
"</style>",
"</head>",
"<body>",
"<h1>Server/Application Debug Page</h1>",
self.get_crumbnav(environ),
"<h2>Browser Headers</h2>",
"<table>"] +
["<tr><td>%s:</td><td>%s</td></tr>" %
(key, val) for key, val in headers.items()] +
["</table>",
"<h2>Application Environment</h2>",
"<table>"] +
["<tr><td>%s:</td><td>%s</td></tr>" %
(key, val) for key, val in environ.items()] +
["</table>",
"<hr>",
"<small>Copyright (c) 2013 Ondřej Tůma. See ",
'<a href="http://poorhttp.zeropage.cz">poorhttp.zeropage.cz</a>'
'</small>.',
"</body>",
"</html>"])
response_headers = [('Content-type', 'text/html'),
('X-Application', 'Simple')]
start_response("200 OK", response_headers)
return [line.encode("utf-8") + b'\n' for line in buff]
def favicon(self, environ, start_response):
icon = b"""
AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAD///8A////AP///wD///8AFRX/Bw8P/24ICP/IAgL/7wAA/+oAAP/GAAD/bQAA/wj///8A
////AP///wD///8A////AP///wD///8A////AB4e/4gYGP//ERH//wsL//8EBP//AAD/1QAA/6AA
AP+C////AP///wD///8A////AP///wD///8A////AP///wAnJ//dISH//xsb//8UFP//Dg7//wcH
/9cBAf+kAAD/1f///wD///8A////AP///wD///8A////AP///wD///8AMTH/4Coq//8kJP//HR3/
/xcX/7AQEP+wCgr/sAMD/5T///8A////AP///wD///8AAHo+BwB3O4AAczjSAHA1jTo6/+AzM///
LS3//yYm//8gIP/YGRn/2BMT/9gNDf/YBgb/2AAA/9UAAP+HAAD/BgB9QGwAej3/AHY6/wBzOKg9
Pf/fPDz//zY2//8vL///KSn//yMj//8cHP//Fhb//w8P//8JCf//AgL//wAA/2oAgELGAH1A/wB5
Pf8AdjrkPD79nz09//89Pf//OTn//zIy//8sLP//JSX//x8f//8YGP//EhL//wwM//8FBf/HAINF
6wCAQv8AfD//AHk8/wtrXYw8PvqUPT3/zz09/9A7O//QNTX/0C4u//koKP//IiL//xsb//8VFf//
Dg7/7wCHR+8Ag0T/AH9B/wB8P/8AeDz/AHU57ABxN7gAbjS4AGoxuABpMLcKYVSFLzP3nysr//8k
JP//Hh7//xcX/+sAiknKAIZH/wCDRP8Af0H/AHs+/wB4PP8AdDn/AHE2/wBtM/8AajH/AGkw/wVl
QZE0NP/xLS3//ycn//8gIP/JAI1MagCJSf8Ahkb/AIJD/wB/Qf8Aez7/AHg7/wB0Of8AcDb/AG0z
/wBpMP8AaTDVPT3/sjY2//8wMP//Kir/egCQTgIAjEuGAIlJ1QCFRugAgkPoAH5A6AB7PugAdzvo
AHQ4/wBwNf8AbDP/AGkw1z09/589Pf/lOTn/mzMz/w7///8A////AP///wD///8AAIVFhQCBQ5gA
fkCYAHo9mAB3O/8Aczj/AG81/wBsMtj///8A////AP///wD///8A////AP///wD///8A////AACI
SN8AhEW0AIFC5wB9QP8Aej3/AHY6/wBzN/8AbzXX////AP///wD///8A////AP///wD///8A////
AP///wAAi0q1AIdHhwCERc4AgEL/AH0//wB5PP8Adjr/AHI3k////wD///8A////AP///wD///8A
////AP///wD///8AAI5NJQCKSpkAh0fhAINE9wCAQvQAfD/fAHk8jwB1ORT///8A////AP///wD/
//8A/D8AAPAPAADwDwAA8A8AAIABAACAAQAAAAAAAAAAAAAAAAAAAAAAAIABAACAAQAA8A8AAPAP
AADwDwAA+B8AAA==
"""
response_headers = [('Content-type', 'image/vnd.microsoft.icon'),
('X-Application', 'Simple')]
start_response("200 OK", response_headers)
return [decodestring(icon)]
def method_not_allowed(self, environ, start_response):
method = environ.get('REQUEST_METHOD')
errors = environ.get('wsgi.errors')
errors.write("Method %s is not allowed for this request\n" % method)
buff = [
"<html>",
"<head>",
'<meta http-equiv="content-type" content="text/html;'
' charset=utf-8"/>',
"<title>405 - Method Not Allowed</title>",
"</head>",
"<body>",
"<h1>405 - Method Not Allowed</h1>",
self.get_crumbnav(environ),
"<p>Method %s is not allowed for this request</p>" % method,
"<hr>",
"<small>Copyright (c) 2013 Ondřej Tůma. See ",
'<a href="http://poorhttp.zeropage.cz">poorhttp.zeropage.cz</a>'
'</small>.',
"</body>",
"</html>"
]
response_headers = [('Content-type', 'text/html'),
('X-Application', 'Simple')]
start_response("405 Method Not Allowed", response_headers)
return [line.encode('utf-8') + b'\n' for line in buff]
def page_not_found(self, environ, start_response):
uri = environ.get('PATH_INFO')
errors = environ.get('wsgi.errors')
errors.write("Your request %s was not found.\n" % uri)
buff = [
"<html>",
"<head>",
'<meta http-equiv="content-type" content="text/html;'
' charset=utf-8"/>',
"<title>404 - Page Not Found</title>",
"</head>",
"<body>",
"<h1>405 - Page Not Found</h1>",
self.get_crumbnav(environ),
"<p>Your request %s was not found.\n</p>" % uri,
"<hr>",
"<small>Copyright (c) 2013 Ondřej Tůma. See ",
'<a href="http://poorhttp.zeropage.cz">poorhttp.zeropage.cz</a>'
'</small>.',
"</body>",
"</html>"
]
response_headers = [('Content-type', 'text/html'),
('X-Application', 'Simple')]
start_response("405 Method Not Allowed", response_headers)
return [line.encode('utf-8') + b'\n' for line in buff]
def error(self, environ, start_response):
raise RuntimeError("Exception error test")
application = Application()
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('127.0.0.1', 8000, application)
httpd.serve_forever()