WSGI Server

2017-11-04  本文已影响0人  idri

三个对象:

详细展开:

Clinet发送请求,Server将App处理的Response返回。以Server的角度,两个动作,接收请求和返回响应**

Client发送一个请求 HTTP Request,Server接受请求转交给 Application,Application处理请求,通过Server返回 HTTP response 给Client,Client加载数据,完成一次HTTP处理流程。**

def simple_app(environ, start_response):
      pass
def simple_app(environ, start_response):
      status = '200 OK'
      response_headers = [('Content-type', 'text/plain')]
      # 先将status,response_headers作为参数返回给start_response
      start_response(status, response_headers) 
      # 再返回body
      return ['hello, world']                                    

服务器把状态,响应头,响应体合并到HTTP响应里,然后传给(HTTP)客户端。

三.两个参数

environ: 由Server通过Request生成。
包含 CGI/WSGI 规范的变量及参数、数据等,比如Client的请求方法、HTTP headers中的content-type内容、HTTP协议版本、WSGI版本等
start_response: 接收两个必选参数和一个可选参数

Middleware

Django的middleware是Django的一种hook机制

Application主要工作:

接受 environ、start_response参数
生成 状态码、headers
返回Response

def app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-Type', 'text/plain')]
    start_response(status, response_headers)
    return ['Hello world from a simple WSGI application!\n']

Django:
python manage.py runserver
自己生成了一个WSGIServer。
django.core.servers.basehttp.get_internal_wsgi_application
->
get_wsgi_application()

上一篇 下一篇

猜你喜欢

热点阅读