打赏

我的WSGI服务器(带路由功能)

效果:

 

 

代码:

from wsgiref.simple_server import make_server


def teacher(environ,start_response):
start_response("200 OK",[('Content-Type','text/html;charset=utf-8')])
return [bytes("<h1>Teacher page</h1>",encoding="utf-8")]

def student(environ,start_response):
start_response("200 OK",[('Content-Type','text/html;charset=utf-8')])
return [bytes("<h1>Student page</h1>",encoding="utf-8")]

def get_url():
data= {'/teacher':teacher,'/student':student}
return data



def run_server(environ,start_response):
urls=get_url()
url=environ.get("PATH_INFO")
print(url)
if url in urls:
function_data = urls[url](environ,start_response)
return function_data
else:
start_response("200 OK",[('Content-Type','text/html;charset=utf-8')])
return [bytes("<h1>error page</h1>",encoding="utf-8")]


server = make_server('localhost',8000,run_server)

server.serve_forever()
posted @ 2019-06-01 10:13  DanielXiaoyc  阅读(321)  评论(0编辑  收藏  举报