wsgi简介

wsgi是什么?

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

  • 创建一个读取时间的py文件
    传入两个参数,一个env,一个报头,根据解析出来的env,作出不同的处理。
#-*- 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方法直接实现更新
    def start_response(self,status,headers):
        '''
         states = "200 OK"
    headers = {
        ("Content-Type","text/plain")
    }
        '''
        response_headers = "HTTP/1.1 " + status +"\r\n"
        for head in headers:
            response_headers += "%s: %s\r\n"%head
        self.response_headers =response_headers

作用

可以方便功能扩展

  • sys进行默认文件夹填充
sys.path.insert(1,WSGI_PYTHON_DIR)
  • 自调用
def __call__(self)

服务器和框架示意图

1652713-0ad7d0be6e22cbe2.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_33778544/article/details/86944885
今日推荐