-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyWebFramework.py
More file actions
79 lines (65 loc) · 2.15 KB
/
myWebFramework.py
File metadata and controls
79 lines (65 loc) · 2.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
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
import time
# from FrameWork.dynamic_web_server import HTTPServer
HTML_ROOT_DIR = "../../html"
class Application(object):
"""
一个通用的响应框架
"""
def __init__(self, a):
self.urls = a
def __call__(self, env, start_response):
path = env.get("PATH_INFO", "/") # 不确定字典中是否有该值,则可以用get方法
if path.startswith("/static"):
file_name = path[7:]
try:
file = open(HTML_ROOT_DIR + file_name, "rb")
except IOError:
start_response(status="404 not found", headers=[])
return "not found"
else:
file_data = file.read()
file.close()
start_response(status="200 OK", headers=[])
return file_data.decode("utf-8")
else:
for url, handler in self.urls:
if path == url:
response_body = handler(env, start_response)
return response_body
start_response(status="404 not found", headers=[])
response_body = "function or file not found"
return response_body
# 补充function信息,补充url到function的映射
def show_c_time(env, start_response):
# 当前调用的模式
env.get("Method")
env.get("PATH_INFO")
# env.get("QUERY_STRING")
# 当前调用的状态和头,用来组成报文头
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
# 返回相应体,在页面中显示的内容
return "You request the time--->"+str(time.ctime())
def say_hello(env, start_response):
env.get("Method")
status = "200 OK"
headers = [
("Content-Type", "text/plain")
]
start_response(status, headers)
# 返回相应体,在页面中显示的内容
return "<---hello--->"
# urls自定义
urls = [
("/", show_c_time),
("/c_time", show_c_time),
("/say_hello", say_hello)
]
app = Application(urls)
# if __name__ == '__main__':
# http_server = HTTPServer(app)
# http_server.bind(8000)
# http_server.start()