# coding:utf-8 from wsgiref.simple_server import make_server #导入simple_server模块 #视图函数 def home(*args): return "home" def login(*args): return "login" def project(*args): return "project" patterns = {"/":home,"/login":login,"/project":project} #路由关系的字典 #定义一个application,遵循wsgi协议; def app(env, start_response):#服务器接收到的客户端请求都会存储在env中,再传入到app进行处理,处理后再返回 url = env.get("PATH_INFO") #从环境变量获取web端传来的url params = env.get("QUERY_STRING") #从环境变量获取web端传入的参数 if (url is None) or (url not in patterns.keys()): start_response("404 not found", [("Content-Type", "text/plain")]) return [b"page not found"] start_response("200 ok",[("Content-Type", "text/plain")]) resp = patterns.get(url) if resp is None: #key在路由关系里,但是velue为none的情况处理 start_response("404 not found", [("Content-Type", "text/plain")]) return [b"page not found"] return [resp(params).encode()] #执行视图函数 #实例化一个服务器设置ip为本机,端口为888,执行程序为上面的app server = make_server("", 5000, app) #开启一个服务器,默认0.5秒轮询,接收客户端请求 server.serve_forever()
启动:python simple_server.py
标签:web,return,python,server,url,found,自带,login From: https://www.cnblogs.com/boye169/p/16942494.html