首页 > Web开发 > 详细

web_wsgi

时间:2020-09-20 23:53:18      阅读:32      评论:0      收藏:0      [点我收藏+]

# __author: "ZXYang"
# date: 2020/9/15
from wsgiref.simple_server import make_server
from week10_web.MyWeb.urls import routers


def f1(request):
return [b‘<h1>Hello, book!</h1>‘]


def f2(request):
return [b‘<h1>Hello, Web!</h1>‘]


# 后期可添加上方法和下面的路径
def routers():
urlpatterns = (
("/book", f1),
("/web", f2),
)
return urlpatterns


def application(environ, start_response):
# environ:所有请求信息的对象;
# start_response:响应信息的对象;方便设置响应头。
start_response(‘200 OK‘, [(‘Content-Type‘, ‘text/html‘)])

# print("environ:", environ)
print("environ:", environ["PATH_INFO"])
path = environ["PATH_INFO"]

urlpatterns = routers()
func = None
for item in urlpatterns:
if item[0] == path:
func = item[1]
break
if func:
return func(environ)
else:
return ["<h1 style=‘color: red‘>404</h1>".encode("utf8")]


# 创建socket对象及准备过程(socket, bind, listen)
httpd = make_server(‘‘, 8080, application)
print(‘Serving HTTP on port 8000‘)
httpd.serve_forever()

web_wsgi

原文:https://www.cnblogs.com/zxy01/p/13702422.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!