仿Openstack的WSGI接口及RESTul服务实现(python)(转)
来源:互联网 发布:苹果换彩铃用什么软件 编辑:程序博客网 时间:2024/06/04 23:30
Openstack的WSGI接口通过webob,pastedeploy,routes实现了Controller类,和Router类,这里仿照Openstack的WSG接口实现简单的测试程序首先是testroutes.py文件
import loggingimport osimport webob.dec import webob.excfrom paste.deploy import loadappfrom wsgiref.simple_server import make_server import routes.middleware # Environment variable used to pass the request contextCONTEXT_ENV = 'openstack.context'# Environment variable used to pass the request paramsPARAMS_ENV = 'openstack.params'LOG = logging.getLogger(__name__)class Controller(object): @webob.dec.wsgify def __call__(self, req): arg_dict = req.environ['wsgiorg.routing_args'][1] action = arg_dict.pop('action') del arg_dict['controller'] context = req.environ.get(CONTEXT_ENV, {}) context['query_string'] = dict(req.params.iteritems()) context['headers'] = dict(req.headers.iteritems()) context['path'] = req.environ['PATH_INFO'] params = req.environ.get(PARAMS_ENV, {}) for name in ['REMOTE_USER', 'AUTH_TYPE']: try: context[name] = req.environ[name] except KeyError: try: del context[name] except KeyError: pass params.update(arg_dict) # TODO(termie): do some basic normalization on methods method = getattr(self, action) result = method(context, **params) return webob.Response('OK') def getMessage(self,context, user_id): return {'Message': 'TestMessage'} pass class Router(object): def __init__(self): self._mapper = routes.Mapper() self._mapper.connect('/test/{user_id}', controller=Controller(), action='getMessage', conditions={'method': ['GET']}) self._router = routes.middleware.RoutesMiddleware(self._dispatch, self._mapper) @webob.dec.wsgify def __call__(self, req): return self._router @staticmethod @webob.dec.wsgify def _dispatch(req): match = req.environ['wsgiorg.routing_args'][1] if not match: return webob.exc.HTTPNotFound() app = match['controller'] return app @classmethod def app_factory(cls, global_config, **local_config): return cls() if __name__ == '__main__': configfile='testroutes.ini' appname="main" wsgi_app = loadapp("config:%s" % os.path.abspath(configfile), appname) httpd = make_server('localhost', 8282, wsgi_app) httpd.serve_forever()
然后是testroutes.ini文件
[DEFAULT]name=huang[composite:main]use=egg:Paste#urlmap/=show[pipeline:show]pipeline = root[app:root]paste.app_factory = testroutes:Router.app_factory
由此可见,ini文件按照pastedeploy的模式配置了根目录/,指向pipeline show,pipeline又指向app root。app下指向的是Router的app_factory函数,返回的是Router().根据调用过程,初始化__init__->__call__返回self._routers.根据__init__下写的映射规则,能识别类似/test/123这样的路径,其处理函数调用"Controller"的"action"函数,也就是getMessage。
测试运行,在浏览器下输入127.0.0,1:8282/test/123
返回OK,如
return webob.Response('OK')
测试完毕。
bluefire1991
2013.11.2
19:59:12
0 0
- 仿Openstack的WSGI接口及RESTul服务实现(python)(转)
- 仿Openstack的WSGI接口及RESTul服务实现(python)
- 0.2 OpenStack-python-wsgi服务
- 戏说WSGI(Python Web服务网关接口)
- 【Python】Web服务器网关接口(WSGI)的简介及wsgiref模块
- OpenStack设计与实现(五)RESTful API和WSGI
- python使用WSGI接口实现简单网页
- Python中WSGI接口的理解
- Openstack源代码分析之keystone部分(一)--WSGI接口流程分析
- Openstack Keystone 认证流程(三)-WSGI
- web python -- WSGI接口
- Python WSGI接口
- python WSGI接口
- Python之WSGI接口
- 【Python】WSGI接口
- 使用Wsgi实现web服务的调用
- python学习笔记(WSGI)
- web python -- WSGI接口响应
- 查询oracle中所有用户信息
- android 函数可变参数
- Duck typing
- java.sql.SQLException:Value ’0000-00-00′ can not be represented as java.sql.Date
- [Java基础要义] Java语言中Object对象的hashCode()取值的底层算法是怎样实现的?
- 仿Openstack的WSGI接口及RESTul服务实现(python)(转)
- enq: TX - index contention
- Hack Like a Pro: How to Kill and Disable Antivirus Software on a Remote PC
- Sicily 1176 Two Ends
- VS2010调用matlab2010b引擎
- TOP域名的特点和优势详解
- Centos vsftp使用
- ORA-01589错误的解决方法
- OJ--求矩阵对角线元素之和