WSGI小程序
来源:互联网 发布:趋势密码软件好吗 编辑:程序博客网 时间:2024/06/05 16:22
模拟openstack中的WSGI,先看一下运行过程中的调用顺序:
[root@jeguan-ctrl-01 WSGI]# python wsgi.py Router app_factoryRouter __init__Router __call__Router _dispatchRouter match = {'action': u'getMessage', 'controller': <wsgi.Controller object at 0x17965d0>, 'user_id': u'123'}Controller __call__Controller action = getMessageController params = {}Controller method = <bound method Controller.getMessage of <wsgi.Controller object at 0x17965d0>>Controller getMessageController result = {'Message': 'TestMessage'}
WSGI server 创建文件:wsgi.py
import loggingimport osimport webob.decimport webob.excfrom paste.deploy import loadappfrom wsgiref.simple_server import make_serverimport routes.middleware# Environment variable used to pass the request context CONTEXT_ENV = 'openstack.context'# Environment variable used to pass the request params PARAMS_ENV = 'openstack.params'LOG = logging.getLogger(__name__)class Controller(object): @webob.dec.wsgify def __call__(self, req): print "Controller __call__" arg_dict = req.environ['wsgiorg.routing_args'][1] action = arg_dict.pop('action') print "Controller action = %s" % 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, {}) print "Controller params = %s" % params 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) print "Controller method = %s" % method result = method(context, **params) print "Controller result = %s" % result return webob.Response('OK') def getMessage(self,context, user_id): print "Controller getMessage" return {'Message': 'TestMessage'} passclass Router(object): def __init__(self): print "Router __init__" 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): print "Router __call__" return self._router @staticmethod @webob.dec.wsgify def _dispatch(req): print "Router _dispatch" match = req.environ['wsgiorg.routing_args'][1] print "Router match = %s" % match if not match: return webob.exc.HTTPNotFound() app = match['controller'] return app @classmethod def app_factory(cls, global_config, **local_config): print "Router app_factory" return cls()if __name__ == '__main__': """ Openstack WSGI interface: create a "Controller" and "Router" class with webob,pastedeploy,routes. Test case for Openstack WSGI. """ configfile='testpaste.ini' appname="main" wsgi_app = loadapp("config:%s" % os.path.abspath(configfile), appname)
paste deploy文件:testpaste.ini
[DEFAULT]name=guan[composite:main]use=egg:Paste#urlmap/=show[pipeline:show]pipeline = root[app:root]paste.app_factory = wsgi:Router.app_factory
从testpaste.init可以看出,程序的入口处:
wsgi:Router.app_factory
0 0
- WSGI小程序
- WSGI 程序 起步
- WSGI
- WSGI
- WSGI
- WSGI
- WSGI
- WSGI
- wsgi
- WSGI
- WSGI
- WSGI
- WSGI
- wsgi
- WSGI
- WSGI
- WSGI
- Django 源码小剖: 初探 WSGI
- Android自定义标题栏
- 编译原理 阅读(3)
- 6、web(js):js中window.location.search的用法和作用。
- qq邮箱smtp服务地址
- C++的升级策略
- WSGI小程序
- AngularJs数据绑定
- Tyvj 1728 普通平衡树
- phpstorm+xdebug断点调试php
- 最近接一个华技CMES协同系统开发对接的项目,顺利完成开发对接
- 编译原理 阅读(1)
- 哪里东西更便宜?读《卧底经济学(珍藏版)》
- map和set
- Android对ListView控件增删改查