# __author: "ZXYang"
# date: 2020/9/15
from wsgiref.simple_server import make_server
from week10_web.MyWeb.urls import routers
def f1(request):
return [b‘<h1>Hello, book!</h1>‘]
def f2(request):
return [b‘<h1>Hello, Web!</h1>‘]
# 后期可添加上方法和下面的路径
def routers():
urlpatterns = (
("/book", f1),
("/web", f2),
)
return urlpatterns
def application(environ, start_response):
# environ:所有请求信息的对象;
# start_response:响应信息的对象;方便设置响应头。
start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)])
# print("environ:", environ)
print("environ:", environ["PATH_INFO"])
path = environ["PATH_INFO"]
urlpatterns = routers()
func = None
for item in urlpatterns:
if item[0] == path:
func = item[1]
break
if func:
return func(environ)
else:
return ["<h1 style=‘color: red‘>404</h1>".encode("utf8")]
# 创建socket对象及准备过程(socket, bind, listen)
httpd = make_server(‘‘, 8080, application)
print(‘Serving HTTP on port 8000‘)
httpd.serve_forever()
web_wsgi