python wsgi
什么是wsgi?
wsgi是一个web组件的接口防范,wsgi将web组件分为三类:web服务器,web中间件,web应用程序
wsgi基本处理模式为:wsgi Server -> wsgi middleware -> wsgi application
wsgi server:
理解为一个符合wsgi规范的web server,接收request请求,封装一系列环境变量,按照wsgi规范调用注册的wsgi app,最后将response返回给客户端。
工作流程:
1、服务器创建socket,监听port,等待client 连接
2、当请求过来时,server解析client msg放到环境变量environ中,并调用绑定的handler来处理
3、handler解析这个http请求,将请求消息例如method、path等放到environ中
4、wsgi handler再将一些server端消息也放到environ中,最后server msg,client msg,以及本次请求msg 全部都保存到了环境变量envrion中;
5、wsgi handler调用注册的wsgi app,并将envrion和回调函数传给wsgi app
6、wsgi app将reponse header/status/body回传给wsgi handler
7、handler 通过socket将response msg返回到client
WSGI Application
wsgi application就是一个普通的callable对象,当有请求到来时,wsgi server会调用这个wsgi app。这个对象接收两个参数,通常为environ,start_response。environ就像前面介绍的,可以理解为环境变量,
跟一次请求相关的所有信息都保存在了这个环境变量中,包括服务器信息,客户端信息,请求信息。start_response是一个callback函数,wsgi application通过调用start_response,
将response headers/status 返回给wsgi server。此外这个wsgi app会return 一个iterator对象 ,这个iterator就是response body。
Dispatcher Middleware,用来实现URL 路由:(代码说明)
#!/usr/bin/python
#encoding=utf-8 #利用wsgiref 作为wsgi server
from wsgiref.simple_server import make_server
"""
def simple_app(environ, start_response):
status = '200 ok'
response_headers = [('Content-type', 'text/plain')] #设置http头
start_response(status, response_headers)
return [u"test wsgi app".encode('utf-8')] class AppClass(object):
def __call__(self, environ, start_response):
status = "200 ok"
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return [u"class AppClass".encode('utf-8')]
""" #wsgi app只要是一个callable对象即可,不一定要是函数
#一个实现了__call__方法示例也ok的 #httpd = make_server('', 8080, simple_app)
"""
app = AppClass()
httpd = make_server('', 8080, app)
httpd.serve_forever()
"""
URL_PATTERNS = (
('AA/', 'AA_app'),
('BB/', 'BB_app'),
) class Dispatcher(object):
#实现路由功能:
def _match(self, path):
path = path.split('/')[1]
for url, app in URL_PATTERNS:
if path in url:
return app def __call__(self, environ, start_response):
path = environ.get('PATH_INFO', '/')
app = self._match(path)
if app:
app = globals()[app]
return app(environ, start_response)
else:
start_response("404 NOT FOUND",[('Content-type', 'text/plain')])
return ["page dose not exists"] def AA_app(environ, start_response):
start_response("200 OK",[('Content-type', 'text/html')])
return ["AA page"] def BB_app(environ, start_response):
start_response("200 OK",[('Content-type', 'text/html')])
return ["BB page"] app = Dispatcher()
httpd = make_server('', 8090, app)
httpd.serve_forever() 测试结果:
server端:
root@u163:~/cp163/python# python wsgi_app.py
192.168.2.162 - - [04/Nov/2015 18:44:06] "GET /AA HTTP/1.1" 200 7
192.168.2.162 - - [04/Nov/2015 18:44:22] "GET /BB HTTP/1.1" 200 7 client端:
root@u162:~# curl http://192.168.2.163:8090/AA
AA page
root@u162:~# curl http://192.168.2.163:8090/BB
BB page
root@u162:~#
上述示例是wsgi application的一个简单模型;
openstack中的wsgi application会涉及到几个比较重要的python 库
- eventlet.wsgi
- paste.deploy
- routes
- webob
- wsgiref.simple_server
开发一个OpenStack 风格的WSGI APP原型需要完成的几方面的工作:
从配置文件中找到WSGI APP程序启动的入口,例如nova的api-paste.ini文件。 在/etc/nova/api-paste.ini
定义好APP需要操作的资源,这儿我们主要是app的版本资源。
完成好url到资源的映射。
python wsgi的更多相关文章
- Python 学习笔记13:Python + wsgi + django 配置。坑爹的python3和wsgi不兼容的解决
今人不见古时月,今月曾经照古人.生命是如此的美丽与短暂! 学习Python已经两个月了,Python的语法通过做简单的语法题和看Python语法介绍,有了初步的了解.但上班还是要做别的事情,所以感觉学 ...
- 在Apache中运行Python WSGI应用
我们介绍如何使用Apache模块mod_wsgi来运行Python WSGI应用. 安装mod_wsgi 我们假设你已经有了Apache和Python环境,在Linux或者Mac上,那第一步自然是安装 ...
- (转)python WSGI框架详解
原文:https://www.cnblogs.com/shijingjing07/p/6407723.html?utm_source=itdadao&utm_medium=referral h ...
- python WSGI框架详解
1.web应用的本质1)浏览器发送一个HTTP请求2)服务器收到请求,生成一个HTML文档3)服务器把HTML文档作为HTTP响应的body发个浏览器4)浏览器收到HTTP响应,从HTTP Body取 ...
- python - wsgi协议
wsgi - python web server gateway interface 出现的目的是,为了在 python框架开发的时候,更具有通用性.只要符合 wsgi标准,就可以自由选择服务器(ng ...
- 对于python WSGI的理解
首先看看WSGI的目的是什么? 是用来定义一个统一的接口. 这个接口是针对Web服务器和python Web应用之间的. 以增加Python web应用在不同Web 服务器之间的可移植性. 也就是说如 ...
- python wsgi 简介
wsgi全称是"Web Server Gateway Interfacfe",web服务器网关接口,wsgi在python2.5中加入,是web服务器和web应用的标准接口,任何实 ...
- Python WSGI接口
WSGI(Web Server Gateway Interface 或 Python Web Server Gateway Interface ),是为 Python 语言定义的 Web 服务器与 W ...
- python wsgi PEP333 中文翻译
PEP 333 中文翻译 首先说明一下,本人不是专门翻译的,英文水平也不敢拿来献丑.只是这是两年前用python的时候为了自己学习方便而翻译的,记录着笔记自己看看而已.最近翻出来看看觉得还是放出来吧. ...
随机推荐
- python基础知识讲解——@classmethod和@staticmethod的作用
python基础知识讲解——@classmethod和@staticmethod的作用 在类的成员函数中,可以添加@classmethod和@staticmethod修饰符,这两者有一定的差异,简单来 ...
- Umbraco安装权限问题
当安装或升级,甚至是使用Umbraco时,如果出现一些莫名其妙的问题. 估计都是权限出了问题,用心参考下文: https://our.umbraco.org/documentation/Getting ...
- core_cm3.c解析
CMSIS是Cortex微控制器软件接口标准(Cortex MicroController Software Interface Standard)的缩写,这个是ARM定制的一个用于Cortex-M系 ...
- window.open打开新页面,并将本页数据用过url传递到打开的页面;需要两个页面;
页面1 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 【每日一摩斯】-Troubleshooting: High CPU Utilization (164768.1) - 系列6
如果问题是一个正运行的缓慢的查询SQL,那么就应该对该查询进行调优,避免它耗费过高的CPU资源.如果它做了许多的hash连接和全表扫描,那么就应该添加索引以提高效率. 下面的文章可以帮助判断查询的问题 ...
- NGINX 多个域名配置
多个域名配置: 依赖于 include 这个功能会加在 这2个文件夹下的所有配置文件. 所以我们可以配置多个 conf 放置于这些文件夹中.这样就是先了多个域名配置 conf 内容大致如下 s ...
- SQL学习之HAVING过滤分组
1.SQL除了能用Group By分组数据之外,SQL还允许过滤分组,规定包括那些分组,排除那些分组.例如,你可能想要列出至少有两个订单的所有顾客.为此,必须基于完整的分组而不是个别的行进行过滤. 基 ...
- SQL2008缩小日志脚本
以下为SQL2008 缩小日志文件的脚本,在SQL Server Management Studio中打开数据库,将脚本里的数据库名称替换成需要缩小日志的库名称,然后 运行以下脚本. USE WSS_ ...
- ##DAY9 UITabBarController
##DAY9 UITabBarController UIViewController的tabBarController UIViewController的tabBarItem #pragma mark ...
- JS中的this都有什么作用?
1.全局代码中的this 是指向全局对象,在浏览器中是window alert(this) //window 2.作为单纯的函数调用: function fooCoder(x) { this.x = ...