WSGI(Python Web Server Grateway Interface)
定义了Web服务器和Web应用程序(APP)之间通信的接口规范
任何一个实现了WSGI应用都可以运行在实现了WSGI协议的服务器上,在没有WSGI的话会限制我们对应用框架的选择。
服务器和应用不再具有强关联。
WSGI应用是一个接受两个参数的可调用的对象。
两个参数:1、environ参数是个字典对象,包含CGI风格的环境变量。
2、start_response参数是一个接受两个固定参数和一个可选参数的可调用者。
def application(environ, start_response): response_body = "Hello World" header = [('Content-Type', 'text/html')] status = "200 OK" start_response(status, header) print("environ http request method:"+environ['REQUEST_METHOD']) return [response_body.encode('utf8')] if __name__ == "__main__": from wsgiref.simple_server import make_server httpd = make_server("0.0.0.0", 8080, application) print("httpd run on :"+str(httpd.server_port)) httpd.serve_forever()
分别以POST、GET方式访问http://127.0.0.1:8080时,在Python中打印:
httpd run on :8080
environ http request method:POST
environ http request method:GET
Flask框架就是一个复杂的WSGI应用