久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

仿Openstack的WSGI接口及RESTul服務實現是怎樣的

167次閱讀
沒有評論

共計 2722 個字符,預計需要花費 7 分鐘才能閱讀完成。

這期內容當中丸趣 TV 小編將會給大家帶來有關 仿 Openstack 的 WSGI 接口及 RESTul 服務實現是怎樣的,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

Openstack 的 WSGI 接口通過 webob,pastedeploy,routes 實現了 Controller 類,和 Router 類,這里仿照 Openstack 的 WSG 接口實現簡單的測試程序
首先是 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

由此可見,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)

上述就是丸趣 TV 小編為大家分享的 仿 Openstack 的 WSGI 接口及 RESTul 服務實現是怎樣的了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注丸趣 TV 行業資訊頻道。

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-17發表,共計2722字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 临颍县| 龙岩市| 修文县| 恩施市| 秦皇岛市| 兰考县| 大田县| 洪江市| 诏安县| 淮安市| 察哈| 渭南市| 天全县| 来凤县| 平罗县| 开江县| 肇东市| 屏东市| 汕尾市| 依兰县| 冕宁县| 榆社县| 千阳县| 吉林省| 湘潭县| 嵩明县| 石狮市| 嵊州市| 夏邑县| 耒阳市| 洞口县| 长乐市| 嘉禾县| 仙居县| 靖宇县| 迁安市| 洛南县| 雷波县| 闸北区| 杭州市| 桐梓县|