wsgi简介 服务器和框架示意图

WSGI:Web Server Gateway Interface。
WSGI接口定义非常简单,它只要求Web开发者实现一个函数,就可以响应HTTP请求。

  • 创建一个读取时间的py文件
    传入两个参数,一个env,一个报头,根据解析出来的env,作出不同的处理。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #-*- conding:utf-8 -*-
    import time
    def applitation(env,start_response):
    # env.get("Method")
    # env.get("Path_Info")
    #
    states = "200 OK"
    headers = {
    ("Content-Type","text/plain")
    }
    start_response(states,headers)
    return time.ctime()
  • 编写回调函数,回调函数一般没有返回值,用self方法直接实现更新

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    def start_response(self,status,headers):
    '''
    states = "200 OK"
    headers = {
    ("Content-Type","text/plain")
    }
    '''
    response_headers = "HTTP/1.1 " + status +"rn"
    for head in headers:
    response_headers += "%s: %srn"%head
    self.response_headers =response_headers

#作用
可以方便功能扩展

  • sys进行默认文件夹填充

    1
    sys.path.insert(1,WSGI_PYTHON_DIR)
  • 自调用

    1
    def __call__(self)

服务器和框架示意图

image.png