這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān) 仿Openstack的WSGI接口及RESTul服務(wù)實(shí)現(xiàn)是怎樣的,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
創(chuàng)新互聯(lián)專注于包河網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供包河營(yíng)銷型網(wǎng)站建設(shè),包河網(wǎng)站制作、包河網(wǎng)頁(yè)設(shè)計(jì)、包河網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開(kāi)發(fā)服務(wù),打造包河網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供包河網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
Openstack的WSGI接口通過(guò)webob,pastedeploy,routes實(shí)現(xiàn)了Controller類,和Router類,這里仿照Openstack的WSG接口實(shí)現(xiàn)簡(jiǎn)單的測(cè)試程序 首先是testroutes.py文件
import logging import os import webob.dec import webob.exc from paste.deploy import loadapp from wsgiref.simple_server import make_server import 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): 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
由此可見(jiàn),ini文件按照pastedeploy的模式配置了根目錄/,指向pipeline show,pipeline又指向app root。app下指向的是Router的app_factory函數(shù),返回的是Router().根據(jù)調(diào)用過(guò)程,初始化__init__->__call__返回self._routers.根據(jù)__init__下寫的映射規(guī)則,能識(shí)別類似/test/123這樣的路徑,其處理函數(shù)調(diào)用"Controller"的"action"函數(shù),也就是getMessage。
測(cè)試運(yùn)行,在瀏覽器下輸入127.0.0,1:8282/test/123
返回OK,如
return webob.Response('OK')
上述就是小編為大家分享的 仿Openstack的WSGI接口及RESTul服務(wù)實(shí)現(xiàn)是怎樣的了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。