# 作者 : 'zhw'
# IDE : PyCharm
# Version:Python3.6
# 创建时间: 2018/5/2 19:34
# coding = 'utf-8'
fromwsgiref.simple_serverimportmake_server
"""
web框架:
MVC: Model(数据库), View(模版文件),Controller(业务处理)
MTV: Model(数据库), Template(模版文件), View(业务处理)
"""
defhandle_index():
# return ["Hello,Index!".encode("utf-8"), ]
importtime
v =str(time.time())
f =open("index.html",mode="rb")
data = f.read()
data = data.replace(b"@bianliang",v.encode("utf-8")) # Model
return[data,]
defhandle_date():
return["Hello,date!".encode("utf-8"),]
URL_DICT = { ## Controller
"/index": handle_index,
"/date": handle_date,
}
defRunServer(environ,start_response):
# environ 客户端发来的所有数据
# start_response 封装要返回给用户的数据,响应头状态
start_response("200 OK",[("Content-Type","text/html")])
current_url = environ["PATH_INFO"]
# if current_url == "/index":
# return handle_index()
# elif current_url == "/date":
# return handle_date()
func =None
ifcurrent_urlinURL_DICT:
func = URL_DICT[current_url]
iffunc:
returnfunc()
else:
return["404".encode("utf-8"),]
if__name__ =="__main__":
httpd = make_server("",8000,RunServer)
print("Server HTTP on port 8000...")
httpd.serve_forever()
领取专属 10元无门槛券
私享最新 技术干货