wsgi

作者: 黑夜的眸 | 来源:发表于2020-03-27 16:09 被阅读0次

Python 的很多知名的Web框架(例如Flask, Django)实际上都是遵从了这个wsgi模型。


from wsgiref.simple_server import make_server


def demo_app(environ,start_response):
    from io import StringIO
    stdout = StringIO()
    print("Hello world!", file=stdout)
    print(file=stdout)
    h = sorted(environ.items())
    for k,v in h:
        print(k,'=',repr(v), file=stdout)
    start_response("200 OK", [('Content-Type','text/plain; charset=utf-8')])
    return [stdout.getvalue().encode("utf-8")]


if __name__ == '__main__':
    httpd = make_server('127.0.0.1', 8000, demo_app)
    httpd.serve_forever()

    # with make_server('', 8000, demo_app) as httpd:
    #     sa = httpd.socket.getsockname()
    #     print("Serving HTTP on", sa[0], "port", sa[1], "...")
    #     import webbrowser
    #     webbrowser.open('http://localhost:8000/xyz?abc')
    #     httpd.handle_request()  # serve one request, then exit

WSGI Server作用

  • 监听HTTP服务端口(TCPServer, 80端口)
  • 接收浏览器端HTTP请求并解析封装成environ环境数据
  • 负责调用应用程序,将environ和start_response方法传入
  • 将应用程序响应的正文封装成HTTP响应报文返回浏览器端

WSGI APP要求

  • 应用程序是一个可调用对象(函数、类都可以)
  • 这个可调用对象应该接收两个参数(environ, start_response)
  • 调用start_response
  • 最后必须返回一个可迭代对象

相关文章

  • python wsgi+Odoo 的启动

    参考:WSGI初探Odoo web 机制浅析python的 WSGI 简介python wsgi 简介 wsgi的...

  • wsgi&uwsgi

    WSGI协议 WSGI, aka Web Server Gateway Interface, WSGI 不是服务器...

  • wsgi简介

    wsgi是什么? WSGI:Web Server Gateway Interface。WSGI接口定义非常简单,它...

  • WSGI简介

    结合案例Python部署 & 示例代码wsgi-demo All in One WSGI WSGI = Pytho...

  • gunicorn使用

    WSGI 了解gunicorn之前先了解WSGI WSGI是Python Web Server Gateway I...

  • flask的deamon简单分析

    代码样例 分析所谓的wsgi应用,wsgi应用一定要有call函数。这样最后才能被wsgi调用,并将wsgi应用处...

  • 关于网络的记录

    WSGI等 WSGI是一种通信协议。WSGI将Web组件分成了三类:Web 服务器(WSGI Server)、We...

  • flask源码分析

    wsgi协议 关于wsgi协议就不赘述了,以下是最简单的符合wsgi的应用 app attriabute app....

  • Flask流程

    回顾一下Flask的流程: WSGI Server 到 WSGI App 图中可以看到HTTP请求都是通过WSGI...

  • WSGI

    settings.py中 WSGI_APPLICATION = 'WebCsdn.wsgi.application...

网友评论

      本文标题:wsgi

      本文链接:https://www.haomeiwen.com/subject/mxnluhtx.html