from wsgiref.simple_server import make_server
import time
def python():
f = open("index.html", "rb")
data = f.read()
return [data]
def c():
f = open("test.html", "rb")
data = f.read()
return [data]
def curr_time():
f = open("time.html", "rb")
data = f.read()
times = time.strftime("%Y-%m-%d %X", time.localtime())
print(times)
data = str(data, "utf8").replace("testtime", str(times))
return [data.encode("utf8")]
def routers():
urlpatterns = (
("/python", python),
("/c", c),
("/time", curr_time)
)
return urlpatterns
def application(environ, start_response):
path = environ["PATH_INFO"]#获取用户请求路径
start_response("200 OK", [("Content-Type", "text/html")])
urlpatterns = routers()
func = None
for item in urlpatterns:
if item[0] == path:
func = item[1]
break
if func:
return func()
else:
return [b"<h1>404</h1>"]
httpd=make_server("127.0.0.1", 8080, application)
print("Serving HTTP on port 8080")
httpd.serve_forever()#开始监听HTTP请求
自建简易web框架
最新推荐文章于 2022-06-29 16:35:33 发布