【Python】Web服务器网关接口(WSGI)的简介及wsgiref模块
2016-10-25 22:18
781 查看
WSGI
Web服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI)是为Python语言定义的Web服务器和Web应用程序或框架之间的一种简单而通用的接口。WSGI有两方:「服务器」或「网关」一方,以及「应用程序」或「应用框架」一方。服务方调用应用方,提供环境信息,以及一个回调函数(提供给应用程序用来将消息头传递给服务器方),并接收Web内容作为返回值。
在Python中有一个WSGI模块——wsgiref。它是用纯Python编写的WSGI服务器的参考实现。所谓「参考实现」是指该实现完全符合WSGI标准,但是不考虑任何运行效率,仅供开发和测试使用。
下面附上一份学习WSGI的代码清单,可以运行在Python 2.7以及3.5。
代码清单
# coding=utf-8 from wsgiref.simple_server import make_server, demo_app def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) return [('<h1>Hello, %s!</h1>' % (environ['PATH_INFO'][1:] or 'web')).encode('utf-8')] if __name__ == '__main__': #httpd = make_server('', 8000, demo_app) httpd = make_server(host='', port=8000, app=application) print("Serving HTTP on port 8000...") httpd.serve_forever()
相关文章推荐
- Python 搭建Web站点之Web服务器网关接口
- [转载]戏说WSGI(Python Web服务网关接口)
- 戏说WSGI(Python Web服务网关接口)
- 戏说WSGI(Python Web服务网关接口)
- 基础入门_Python-模块和包.为wsgiref实现的WSGI服务器编写处理函数?
- Python 搭建Web站点之Web服务器网关接口
- 非Python的Web服务器之Apache WSGI模块安装
- 使用Python WSGI编写Web服务器前端,并利用Boost.Python在同一进程内连接到C++服务后端实现
- 详解Python程序与服务器连接的WSGI接口
- web python -- WSGI接口响应
- Python:HTTP请求模块(httplib/urllib,GET/POST,php),Web服务器模块
- 详解Python程序与服务器连接的WSGI接口
- 【脚本语言系列】关于PythonWeb客户端-服务器模块http-urllib,你需要知道的事情
- 戏说WSGI(Python Web服务网关接口)--[转载]
- Python的Web开发之WSGI接口
- 【脚本语言系列】关于PythonWeb服务器apache+mod_wsgi,你需要知道的事
- python的web服务器模块
- Python语言写成的Web服务器 Tornado 简介
- web python -- WSGI接口GET请求
- 搭建Apache+web.py+mod_wsgi python服务器