标签:http start code bre none app XML from bind
# _author:来童星
# date:2020/2/20
# wsgi 框架
from wsgiref.simple_server import make_server
# environ为一个对象,封装了客户端的请求信息(environ是一个包含所有请求信息的dict对象)
# start_response为服务器发送给浏览器(客户端)的响应信息
def f1(request):
return [b‘<h1>hello star</h1>‘]
def f2(request):
return [b"<h1>hello light</h1>"]
def login(request):
print(request)
return [b"<h1>hello login</h1>"]
def routers():
urlpatterns=(
(‘/star‘,f1),
(‘/light‘,f2),
(‘/login‘,login),
)
return urlpatterns
def application(environ, start_response):
# print(‘environ‘,environ)#为一个字典
# environ {‘USERPROFILE‘: ‘C:\\Users‘, ‘HTTP_ACCEPT‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8‘,..}
# print(‘PATH_INFO:‘,environ[‘PATH_INFO‘])# 127.0.0.1 - - [20/Feb/2020 17:32:24] "GET /star/come/up HTTP/1.1" 200 18
start_response("200 ok ", [(‘Content-Type‘, ‘text/html‘)])
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>404</h1>".encode(‘utf8‘)]
# wsgi作用:
# 1.wsgi帮我们封装了socket对象以及准备过程(准备过程包括 socket对象的创建,bind,listen)
# 2.通过environ将所有请求信息封装成一个对象
# 3.通过start_response可以很方便的设置响应头
httpd = make_server(‘‘, 8080, application)
print(‘sereing HTTP is running on port 8080 ‘)
# 开始监听HTTP请求
httpd.serve_forever()
标签:http start code bre none app XML from bind
原文地址:https://www.cnblogs.com/startl/p/12336996.html