WSGI Server

三个对象:

  • HTTP Client(浏览器)
  • HTTP Server (uWSGI、gunicorn)
  • FrameWork/Application (Flask、Pyramid、Django)

详细展开:

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

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

  • 接受请求: Server收到Request,调用(invoke)来自App的「application」对象,Server通过解析Request生成「application」对象的参数,一个字典-environ 一个可调用对象start_response,这个对象就是App的唯一入口。
def simple_app(environ, start_response):
      pass
  • 返回响应:
    App将status、headers、body,返回给Server
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: 接收两个必选参数和一个可选参数

  • status: 一个字符串,表示HTTP响应状态字符串
  • response_headers: 一个列表,包含有如下形式的元组:(header_name, --- header_value),用来表示HTTP响应的headers
  • exc_info(可选): 用于出错时,server需要返回给浏览器的信息

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()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,083评论 19 139
  • Refer to: www.threemeal.com/blog/12/ 中间件 中间件是一个钩子框架,它们可以介...
    兰山小亭阅读 16,590评论 9 165
  • 全称为Web Server Gateway Interface,即 Web服务器网关接口。是一种标准接口规范,规定...
    超net阅读 3,208评论 0 2
  • web静态服务器 服务端: 1.1.1显示固定的页面 参考代码: import socket from multi...
    chen_000阅读 2,106评论 0 1
  • 因为公司转型,现在有一个之前做专利的人,但是现在我们专业这一块儿没有再继续做了。我作为他的直接领导,应该每周给他安...
    AI狼阅读 359评论 0 0