1、说明
WSGI协议的主要目的是规范数据分析格式,如果web服务符合WSGI协议,则其作用是将原始socket数据分析为environ对象(使用时为字典对象)
2、实例
python手册的案例,wsgiref是框架,现在定义app函数和其他可调用类型,将environ和start_response传递给app,最后将app可调用类型传递给框架wsgiser框架make_server。
fromwsgiref.utilimportsetup_testing_defaults fromwsgiref.simple_serverimportmake_server #ArelativelysimpleWSGIapplication.It'sgoingtoprintoutthe #environmentdictionaryafterbeingupdatedbysetup_testing_defaults ############################################################# #主要给app传递environ和start_response函数 ############################################################# defsimple_app(environ,start_response): setup_testing_defaults(environ) status='200OK' headers=[('Content-type','text/plain;charset=utf-8')] start_response(status,headers) ret=[("%s:%s\n"%(key,value)).encode("utf-8") forkey,valueinenviron.items()] returnret httpd=make_server('',8000,simple_app) print("Servingonport8000...") httpd.serve_forever()
以上就是python中WSGI的工作原理,希望对大家有所帮助。更多Python学习指路:Python基础教程
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)