from wsgiref.simple_server import make_server
# demo_app 需要两个参数
# 第0个参数,表示请求路径相关的环境
# 第1个参数,用来返回响应头
# 这个函数需要一个返回值,返回值是一个列表
# 列表里只有一个元素,是一个二进制,表示返回给浏览器的数据
def demo_app(environ,start_response):
# environ是一个字典,保存了很多的数据
# PATH_INFO : 能够获取到用户的访问路径
path = environ['PATH_INFO']
print('path={}'.format(path))
start_response('200 OK',[('Content-Type','text/html;charset=utf8')])
return ['你好'.encode('utf8')] # 浏览器显示的内容
if __name__=='__main__':
# demo_app是一个函数,用来处理用户的请求
httpd = make_server('',8090,demo_app)
sa = httpd.socket.getsockname()
print("Serving Http on", sa[0],"port", sa[1],"...")
httpd.serve_forever()
# 代码的作用是打开电脑的浏览器,并在浏览器里输入http://localhost:8080/xyz?abc
# import webbrowser
# webbrowser.open('http://localhost:8000/xyz?abc')
# httpd.handle_request() # serve one request, then exit
# httpd.handle_request() # 处理一次请求
httpd.serve_forever() # 服务器在后台一直运行
Python:自定义WSGI服务器
猜你喜欢
转载自blog.csdn.net/weixin_42161670/article/details/114786329
今日推荐
周排行