WSGI小程序
模擬openstack中的WSGI,先看一下運行過程中的調用順序:
[root@jeguan-ctrl-01 WSGI]# python wsgi.py Router app_factory Router __init__ Router __call__ Router _dispatch Router match = {'action': u'getMessage', 'controller': <wsgi.Controller object at 0x17965d0>, 'user_id': u'123'} Controller __call__ Controller action = getMessage Controller params = {} Controller method = <bound method Controller.getMessage of <wsgi.Controller object at 0x17965d0>> Controller getMessage Controller result = {'Message': 'TestMessage'}WSGI server 創建文件:wsgi.py import logging import osimport webob.dec import webob.exc from paste.deploy import loadapp from 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.wsgifydef __call__(self, req):print "Controller __call__"arg_dict = req.environ['wsgiorg.routing_args'][1]action = arg_dict.pop('action')print "Controller action = %s" % actiondel 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" % paramsfor name in ['REMOTE_USER', 'AUTH_TYPE']:try:context[name] = req.environ[name]except KeyError:try:del context[name]except KeyError:passparams.update(arg_dict)# TODO(termie): do some basic normalization on methods method = getattr(self, action)print "Controller method = %s" % methodresult = method(context, **params)print "Controller result = %s" % resultreturn 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.wsgifydef __call__(self, req):print "Router __call__"return self._router@staticmethod@webob.dec.wsgifydef _dispatch(req):print "Router _dispatch"match = req.environ['wsgiorg.routing_args'][1]print "Router match = %s" % matchif not match:return webob.exc.HTTPNotFound()app = match['controller']return app@classmethoddef 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轉載于:https://www.cnblogs.com/double12gzh/p/10166119.html
總結
- 上一篇: Activiti操作数据库中文乱码
- 下一篇: linux中chmod与chown两个命