我的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()

猜你喜欢

转载自www.cnblogs.com/xiaoyichong/p/10958703.html
今日推荐