品易云推流 关闭
文章详情页
文章 > Python基础教程 > python中WSGI的工作原理

python中WSGI的工作原理

头像

小妮浅浅

2021-06-01 09:59:462983浏览 · 0收藏 · 0评论

1、说明

WSGI协议的主要目的是规范数据分析格式,如果web服务符合WSGI协议,则其作用是将原始socket数据分析为environ对象(使用时为字典对象)

2、实例

python手册的案例,wsgiref是框架,现在定义app函数和其他可调用类型,将environ和start_response传递给app,最后将app可调用类型传递给框架wsgiser框架make_server。

from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
 
# A relatively simple WSGI application. It's going to print out the
# environment dictionary after being updated by setup_testing_defaults
#############################################################
#主要给app传递environ和start_response函数
#############################################################
def simple_app(environ, start_response):
    setup_testing_defaults(environ)
 
    status = '200 OK'
    headers = [('Content-type', 'text/plain; charset=utf-8')]
 
    start_response(status, headers)
 
    ret = [("%s: %s\n" % (key, value)).encode("utf-8")
           for key, value in environ.items()]
    return ret
 
httpd = make_server('', 8000, simple_app)
print("Serving on port 8000...")
httpd.serve_forever()

以上就是python中WSGI的工作原理,希望对大家有所帮助。更多Python学习指路:python基础教程

本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

底部广告图