forked from pahaz/Example-of-MVC-pattern-on-pure-Python-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsgi.py
More file actions
91 lines (75 loc) · 2.92 KB
/
wsgi.py
File metadata and controls
91 lines (75 loc) · 2.92 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
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
import wsgiref.validate
import router as router_module
from utils import parse_http_x_www_form_urlencoded_post_data, \
get_first_element, parse_http_get_data, parse_http_headers, \
parse_http_content_type, parse_http_uri
DEBUG = True
STATIC_URL = '/static/'
STATIC_ROOT = 'data/'
router = router_module.Router()
data_messages = [
b'Name: user<br>Message: hi!',
b'Name: user<br>Message: hi!',
]
@wsgiref.validate.validator
def application(environ, start_response):
# https://www.python.org/dev/peps/pep-3333/#environ-variables
REQUEST_METHOD = environ['REQUEST_METHOD']
CONTENT_TYPE, CONTENT_TYPE_KWARGS = parse_http_content_type(environ)
SERVER_PROTOCOL = environ['SERVER_PROTOCOL']
HEADERS = parse_http_headers(environ)
URI_PATH = environ['PATH_INFO']
URI_QUERY = environ['QUERY_STRING']
URI = parse_http_uri(environ)
POST = parse_http_x_www_form_urlencoded_post_data(environ)
GET = parse_http_get_data(environ)
status = '200 OK'
headers = [('Content-type', 'text/html; charset=utf-8')]
callback = router.resolve(URI_PATH)
status, body = callback
if URI_PATH == '/favicon.ico':
status = '404 Not Found'
start_response(status, headers)
return [b'']
if URI_PATH.startswith(STATIC_URL):
# import os
import os.path
path = URI_PATH[len(STATIC_URL):]
normpath = os.path.normpath(path)
# print(normpath)
download_path = STATIC_ROOT + normpath
# print(download_path)
if not os.path.exists(download_path):
start_response("404 Not found", headers)
return [b'']
else:
headers = [('Content-type', 'text/plain; charset=utf-8')]
start_response(status, headers)
return [open(download_path, 'rb').read()]
# with open(path, 'rb') as f:
# start_response(status, headers)
# return [f.read()]
DEBUG = False
if DEBUG:
print("{REQUEST_METHOD} {URI_PATH}?{URI_QUERY} {SERVER_PROTOCOL}\n"
"CONTENT_TYPE: {CONTENT_TYPE}; {CONTENT_TYPE_KWARGS}\n"
"POST: {POST}\n"
"GET: {GET}\n"
":HEADERS:\n{HEADERS}\n"
.format(**locals()))
with open('main.html', 'rb') as f:
template_bytes = f.read()
if REQUEST_METHOD == 'POST':
status = '303 See Other'
headers.append(('Location', '/'))
name = get_first_element(POST, 'name', '')
message = get_first_element(POST, 'message', '')
data_message_text = "Name: {0}<br>Message: {1}".format(name, message)
data_message_bytes = data_message_text.encode('utf-8')
data_messages.append(data_message_bytes)
start_response(status, headers)
return [b'']
messages = b'<hr>'.join(data_messages)
template_bytes = template_bytes.replace(b'{{messages}}', messages)
start_response(status, headers)
return [template_bytes]