OpenStack点滴02-WSGI
OpenStack对外提供REST API,那么REST API是如何建立起来的呢?
OpenStack用了一种叫WSGI(Web Service Gateway Interface)的东西,WSGI是Web服务器与Web应用程序或应用框架之间的一种低级别的接口。
下面是个简单的例子:
#!/usr/bin/env python from wsgiref.simple_server import make_server def hello_world_app(environ, start_response):
status = '200 OK' # HTTP Status
headers = [('Content-type', 'text/plain')] # HTTP Headers
start_response(status, headers) # The returned object is going to be printed
return ["Hello World"] httpd = make_server('', 8088, hello_world_app)
print "Serving on port 8088..." # Serve until process is killed
httpd.serve_forever()
执行这段代码,然后浏览器访问http://localhost:8088就会出现Hello World页面。
大致流程如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeUAAABXCAIAAAC1J8r/AAAPw0lEQVR4nO2d21dTVx7H9/8RH+ApD3nXZ/6A6FteGOzMaJ1WOun0IlBjnWq9dLWGWtRehh6vwSCEi5eOhcYL1tZK6azFzIJl1AASAtjaVe1UGRFlHrbd7OxzciA5++ydc9b3u74Ph3Nn78/vey45ySGLEARBkBdEdO8ABEEQtCIhryEIgrwh5DUEQZA3hLyGIAjyhpDXEARB3tDyeT374PHZ4Xu7zk281n7rT0dGa1tH4NrWkReMkdfab+06N9H9w0+zDx4r6KqKFQgBIfYCIbIIscvriXtz75wZr09kdp4e+/RiLnF1umvwbu/38N3e7+92Dd5t+2bm04u53WfH6xOZv58evzHzUBLenhEIASH2AiFyCbHO64Wnz1qv5P9y/EZL32Tv4F14WR/sn9ycyBy8mJtfeOYC9hUnEFIeIYcuToEQuGxCLPL64eOFHWfGY13Z1LVZ7f+Dh9x1/e72nrFYd/b+oyduFoJ+gRAHhGS39YAQuExCxLxeePpsx5nxHb1jPYN34TK85+zE1u6sj8+hQIhDv3duItYDQuByCBHzuvVKfmtXtmdwFi7bb3dnW9I5KejncjlCSC63zNoMw4hEIlK2uKxAiHNv78keuCCHELNWCEMoFOrv73djB0CIe4QU5PXEvbmXTmQ6r830XJ+Fy3bXdzOvtGVGp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v91K25+adKSkODQIhDb+8efwOEwGURYvFqzvuPnjR1ZRs6brf05Q70T8ErdEtfrrHz9uunbt1/9MT9otApEFI2IU2p7BsgBC6XEOtXKc/NP/0oPbk5kdnZO36gbwpe1rtOT9S3Zd4/f8fH5028QEg5hCRACOyIEOu8phqd/q0xla1PZN7qzO49e+eDLyY/+jLX0jcFU3/wxeR7Z+80dY5FT958s+OWLz8+shcIASH2AiFyCbHLa6qJe3Onvp/d1jNW35ZZb+h/gUWFuO7z0fq2zLaesfbBWZ89RVuqQAgIsRcIkUXI8nkNQRAEVYKQ1xAEQd4Q8hqCIMgbQl5DEAR5Q8hrCIIgbwh5DUEQ5A39HxJvUhNachWNAAAAAElFTkSuQmCC" alt="" />
(1)Client(上例中浏览器)发送请求到Server。
(2)Server转发请求给Application(上例中hello_world_app)。注:Server和Application之间还有middleware,此处省略。
(3)Application进行操作后将相应发送给Server。
(4)Server再将相应转发给Client。
OpenStack使用WSGI的一个工具包paste来配置WSGI appliaction和server的系统,它的好处是将配置和代码分离。python代码写好后如果想要修改页面到app的映射关系,只需要修改配置文件即可。
用一个简单的例子来示范paste.deploy的工作机制:
pastedeploy.ini
[composite:test_composite]
use=egg:Paste#urlmap
/:root
[pipeline:root]
pipeline = logrequest showversion
[filter:logrequest]
username = root
password = root123
paste.filter_factory = pastedeploylab:LogFilter.factory
[app:showversion]
version = 1.0.0
paste.app_factory = pastedeploylab:ShowVersion.factory
app:表示它定义了一个wsgi的application,是一个callable对象。paste. app_factory返回值是一个application对象
filter:表示这个段定义了一个filter,filter需要完成的工作是将application包装成另一个application(“过滤”),并返回这个包装后的application。
pipeline:Pipeline 由一些列的filter组成,最后一个是应用,即将前面的fiiter应用到application。
composite:自己不处理请求,根据映射关系把请求分发到filter、app或者pipeline。/:root就是表示访问url根目录的请求全部分发到root这个pipeline处理
pastedeploy.py
import os
import webob
from webob import Request
from webob import Response
from paste.deploy import loadapp
from wsgiref.simple_server import make_server
#Filter
class LogFilter():
def __init__(self,app):
self.app = app
pass
def __call__(self,environ,start_response):
print "filter:LogFilter is called."
return self.app(environ,start_response)
@classmethod
def factory(cls, global_conf, **kwargs):
print "in LogFilter.factory", global_conf, kwargs
return LogFilter
class ShowVersion():
def __init__(self):
pass
def __call__(self,environ,start_response):
start_response("200 OK",[("Content-type", "text/plain")])
return ["Paste Deploy LAB: Version = 1.0.0",]
@classmethod
def factory(cls,global_conf,**kwargs):
print "in ShowVersion.factory", global_conf, kwargs
return ShowVersion()
if __name__ == '__main__':
configfile="pastedeploy.ini"
appname="test_composite"
wsgi_app = loadapp("config:%s" % os.path.abspath(configfile), appname)
server = make_server('localhost',8080,wsgi_app)
server.serve_forever()
pass
执行命令python pastedeploy.py,然后在浏览器中输入http://localhost:8080/就可以在网页输出Paste Deploy LAB: Version = 1.0.0
下面讲解一下工作流程,运行pastedeploy.py文件,首先会调用loadapp函数加载运用,在配置文件pastedeploy.ini找到appname为test_composite,test_composite是一个composite,然后找到pipeline root,根据pipeline找到filter logrequest和app showversion,logrequest和showversion各自用factory生成callable对象。加载完应用后调用make_server启动服务。
在浏览器输入http://localhost:8080/就会根据urlmap将请求分发到pipeline root,调用LogFilter的__call__方法,其中app就是ShowVersion,然后调用ShowVersion的__call__方法返回消息。
以下写一个简单的OpenStack WSGI实例,参考了臭蛋的博客,臭蛋写的和OpenStack源码很一致。
其中用到的一些python库:
1. paste.deploy 配置WSGI appliaction和server
2. webob 用来对http请求和响应进行封装
3. routes 实现URL映射
4. eventlet.wsgi 或者 wsgiref.simple_server,提供wsgi server功能,后者更简单。
首先建立一个test包,然后在test包里面建立如下文件:
test-paste.ini
[composite:test_composite]
use=egg:Paste#urlmap
/v1:testapp [app:testapp]
paste.app_factory = test.router:API.factory
server.py
import os
import logging
import sys
from paste import deploy
from wsgiref.simple_server import make_server LOG = logging.getLogger(__name__) module_dir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir,os.pardir)) sys.path.insert(0,module_dir) bind_host = "127.0.0.1"
bind_port = 8088 def server(app_name, conf_file):
print "server"
app = load_paste_app(app_name,conf_file)
serve = make_server(bind_host,bind_port,app)
serve.serve_forever() def load_paste_app(app_name, conf_file):
print "load_paste_app"
LOG.debug("Loading %(app_name) from %(conf_file)",
{'app_name':app_name, 'conf_file':conf_file}) try:
app = deploy.loadapp("config:%s" % os.path.abspath(conf_file), name=app_name)
return app
except (LookupError, ImportError) as e:
LOG.error(str(e))
raise RuntimeError(str(e)) if __name__ == '__main__':
app_name = "test_composite"
conf_file = "test-paste.ini"
server(app_name,conf_file)
wsgi.py
import logging
import routes.middleware
import webob.dec
import webob.exc class Router(object): def __init__(self, mapper=None):
print "Router.__init__"
self.map = mapper
self._router = routes.middleware.RoutesMiddleware(self._dispatch,
self.map)
@classmethod
def factory(cls, global_conf, **local_conf):
print "Router.__factory__"
return cls() @webob.dec.wsgify
def __call__(self,req):
print "Router.__call__"
return self._router @staticmethod
@webob.dec.wsgify
def _dispatch(req):
print "Router._dispatch"
# TODO
match = req.environ['wsgiorg.routing_args'][1]
if not match:
return webob.exc.HTTPNotFound()
app = match['controller']
return app
router.py
import routes from test import wsgi
from test import versions class API(wsgi.Router): def __init__(self, mapper=None):
print "API.__init__"
if(mapper == None):
mapper = routes.Mapper() versions_resource = versions.create_resource()
mapper.connect("/test",controller=versions_resource,
action="index")
super(API,self).__init__(mapper)
versions.py
import httplib
import json
import webob.dec
from test import wsgi
from webob import Response class Controller(object):
def __init__(self):
print "Controller.__init__"
# TODO
self.version = "0.1" def index(self,req):
print "Controller.index"
response = Response(request=req,
status=httplib.MULTIPLE_CHOICES,
content_type='application/json')
response.body = json.dumps(dict(versions=self.version))
return response @webob.dec.wsgify
def __call__(self, request):
print "Controller.__call__"
# TODO
return self.index(request) def create_resource():
print "create_resource"
return Controller()
@webob.dec.wsgify 装饰器将一个普通函数转变成WSGI应用程序
执行python server.py , 然后在浏览器输入http://localhost:8088/v1/test 就会出现相关页面。
由于在函数中加了打印语句,启动时会输出:
server
load_paste_app
Router.__factory__
API.__init__
create_resource
Controller.__init__
Router.__init__
访问页面会输出:
Router.__call__
Router._dispatch
Controller.__call__
Controller.index
这是一个OpenStack WSGI原型,还需完善,比如在router.py文件中,/test并没有和index方法绑定,只是在Controller.__call__方法中静态的调用了index方法。
OpenStack点滴02-WSGI的更多相关文章
- OpenStack中给wsgi程序写单元測试的方法
在 OpenStack 中, 针对web应用, 有三种方法来写单元測试 1) 使用webob生成模拟的request from __future__ import print_function imp ...
- OpenStack点滴03-Neutron
OpenStack Neutron是一个比较复杂的组件,如果说OpenStack安装部署过程哪一个组件最麻烦,那就肯定是网络组件Neutron了. 因为我对OpenStack网络这块也不是很熟,就把我 ...
- OpenStack点滴01-概览
OpenStack项目由Rackspace和NASA(美国国家航空航天局)共同发起,它是一个旨在为公共及私有云的建设与管理提供软件的开源项目. 作为一个开源云平台,OpenStack的首要任务是简化云 ...
- OpenStack Trove组件WSGI和RPC调用流程(参考调用的整个过程)
参考博文:https://blog.csdn.net/bill_xiang_/article/details/72909927
- python wsgi
什么是wsgi? wsgi是一个web组件的接口防范,wsgi将web组件分为三类:web服务器,web中间件,web应用程序 wsgi基本处理模式为:wsgi Server -> wsgi m ...
- openstack学习笔记一 虚拟机启动过程代码跟踪
openstack学习笔记一 虚拟机启动过程代码跟踪 本文主要通过对虚拟机创建过程的代码跟踪.观察虚拟机启动任务状态的变化,来透彻理解openstack各组件之间的作用过程. 当从horizon界面发 ...
- openstack学习心得:keystone 架构、概念、访问流程
1.keystone 介绍及其组成 OpenStack Identity 服务提供了一个单一的功能集合,包括管理认证,授权和服务目录. Identity 服务通常作为和用户第一个交互的服务.一旦认证成 ...
- keystone WSGI流程
作为OpenStack两种基本的通信方式(RESTful API与消息总线)之中的一个.理解RESTful API的设计思路和运行过程,有助于我们对OpenStack有更好的理解.RESTful仅仅是 ...
- Ubuntu16手动安装OpenStack——nova篇。。转
前言: 本文转自https://www.voidking.com/dev-ubuntu16-manual-openstack-nova/ ,过程非常的详细,作者也说本实验最终失败,因为课程要求我们只要 ...
随机推荐
- -_-#【Canvas】measureText, translate, drawImage
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- SET NOCOUNT (Transact-SQL)
阻止在结果集中返回显示受 Transact-SQL 语句或存储过程影响的行计数的消息. 语法 SET NOCOUNT { ON | OFF } 注释 当 SET NOCOUNT 为 ON 时,不返回 ...
- HDOJ 2117 Just a Numble(模拟除法)
Problem Description Now give you two integers n m, you just tell me the m-th number after radix poin ...
- Java8 时间 API
前言 Java8 中最为人津津乐道的新改变恐怕当属函数式 API 的加入.但实际上,Java8 所加入的新功能远不止这个. 本文将基于<Java SE8 for the Really Impat ...
- python使用正則表達式
python中使用正則表達式 1. 匹配字符 正則表達式中的元字符有 . ^ $ * + ? { } [ ] \ | ( ) 匹配字符用的模式有 \d 匹配随意数字 \D 匹配随意非 ...
- mysql 查询某字段里含有(或者不含)某字符的所有记录方法(转)
select gid, username from users where FIND_IN_SET(8,gid); //查询gid里含有数字8的记录,gid是varchar ,数据格式:"1 ...
- 【移动开发】Android中WIFI开发总结(一)
WIFI就是一种无线联网技术,常见的是使用无线路由器.那么在这个无线路由器的信号覆盖的范围内都可以采用WIFI连接的方式进行联网.如果无线路由器连接了一个ADSL线路或其他的联网线路,则又被称为“热 ...
- Java基础知识强化之集合框架笔记01:集合的由来与数组的区别
1. 集合的由来: 我们学习的是面向对象语言,而面向对象语言对事物的描述是通过对象体现的,为了方便对多个对象进行操作,我们就必须把这多个对象进行存储.而要想存储多个对象,就不能是一个基本的变量,而应该 ...
- systemd添加自定义系统服务设置自定义开机启动
1.服务权限 systemd有系统和用户区分:系统(/user/lib/systemd/system/).用户(/etc/lib/systemd/user/).一般系统管理员手工创建的单元文件建议存放 ...
- 【转】iOS实时卡顿监控
转自http://www.tanhao.me/code/151113.html/ 在移动设备上开发软件,性能一直是我们最为关心的话题之一,我们作为程序员除了需要努力提高代码质量之外,及时发现和监控软件 ...