WSGI(Web Server Gateway Interface)服务器网关接口,是为Python语言定义的Web服务器和Web应用程序或框架之间的通用接口。WSGI是基于CGI标准设计的,需要明确的是,WSGI是一种服务器和客户端交互的接口规范,并不是API,不是Python模块,也不是框架。
WSGI Server:接收请求
environ ###环境信息
start_response ###回调函数,返回response_headers数据以及status信息
WSGI App:处理请求,返回参数给Server
接收服务端的environ和start_response
回调函数接收两个参数response_headers,status
通过回调函数,将response_headers,status返回给服务端
返回一个迭代对象,response.body
WSGI MiddleWare:
实现URL路由。对应用程序包装,实现各种功能。
下面通过利用Python框架,实现一个简单的Web应用程序来记录系统时间
vim /var/www/cgi-bin/webapp.wsgi
#!/usr/bin/env python
import time
def application (environ, start_response):
response_body = 'UNIX EPOCH time is now: %s\n' % time.time()
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', '1'),
('Content-Length', str(len(response_body)))]
start