搜索
您的当前位置:首页python中WSGI的工作原理

python中WSGI的工作原理

时间:2024-07-17 来源:乌哈旅游

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学习指路:

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

Top