WSGI是pythonWEB编程的接口 相当于Java的servlet规范
服务器根据规范进行底层网络编程 封装HTTP协议
WEB应用开发更加规范编写应用
由服务器来运行WEB应用
根据WSGI编写一个小程序
def application(environ, start_response):
start_response('200 ok', [('Content-Type', "text/html")])
return [b'<h1>Hello Web!</h1>']
用python中的wsgiref启动一个服务器
from wsgiref.simple_server import make_server
from mark.zhou.my_app import application
httpd = make_server('0.0.0.0', 8080, application)
print('Server HTTP on port 8000...')
httpd.serve_forever()
----------------------------成功访问--------
C:\Python\Python36\python.exe D:/IdeaProjects/python_basic/mark/zhou/my_simple_server.py
This is mark __init__.py
Server HTTP on port 8000...
127.0.0.1 - - [03/Oct/2017 16:57:03] "GET / HTTP/1.1" 200 19
127.0.0.1 - - [03/Oct/2017 16:57:03] "GET /favicon.ico HTTP/1.1" 200 19
127.0.0.1 - - [03/Oct/2017 16:57:03] "GET /favicon.ico HTTP/1.1" 200 19
def application(environ, start_response):
- environ 封装了HTTP请求的内容
start_response 封装了响应的内容
用environ从PATH_INFO中读取路径参数
def application(environ, start_response):
start_response('200 ok', [('Content-Type', "text/html")])
body = "<h1>Hello {}!</h1>".format(environ['PATH_INFO'][1:] or 'web')
return [body.encode("utf-8")]