forked from shiyanlou/louplus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (33 loc) · 1.15 KB
/
app.py
File metadata and controls
45 lines (33 loc) · 1.15 KB
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
from wsgiref.simple_server import make_server
def index():
return b'index page', '200 OK', [('Content-Type', 'text/html')]
def api():
return b'{"name": "Aiden", "email": "luojin@simplecloud.cn"}', '201 Created', [('Content-Type', 'application/json')]
def not_found():
return b'404 page', '404 NOT FOUND', [('Content-Type', 'text/plain')]
URL_PATTERNS= (
('/', index),
('api', api),
('course', not_found)
)
class Flask:
def route(self, path):
path = path.split('/')[1]
for url, controller in URL_PATTERNS:
if path in url:
return controller
def __call__(self, environ, start_response):
path = environ.get('PATH_INFO','/')
controller = self.route(path)
if controller :
body, status, headers = controller()
start_response(status, headers)
return [body]
else:
start_response('404 NOT FOUND',[('Content-type', 'text/plain')])
return [b'Page dose not exists!']
if __name__ == '__main__':
app = Flask()
httpd = make_server('', 8091, app)
print('Serving on port 8091...')
httpd.serve_forever()