from wsgiref.simple_server import make_server
def book(request): #视图函数
return [b‘<h1> book !!! </h1>‘]
def web(request):
return [b‘<h1> web !!! </h1>‘]
def routers():
urlpatterns = (
(‘/book‘,book), #url 映射
(‘/web‘,web),
)
return urlpatterns
def application(environ,start_response):
start_response("200 OK",[("Content-Type",‘text/html‘)])
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 [b"<h1> 404 </h1>"]
httpd = make_server(‘127.0.0.1‘,9000,application)
print("Serving HTTP on port 9000....")
httpd.serve_forever()