openstack 中的Routes + webob 的 REST API

转载 2014年11月12日 00:29:28

首先贴一段代码

from __future__ import print_function
from routes import Mapper
import webob.dec
import webob.exc
import routes.middleware
import testtools

class MyController(object):
    def getlist(self, mykey):
        print("step 4: MyController's getlist(self, mykey) is invoked")
        return "getlist(), mykey=" + mykey

class MyApplication(object):
    """Test application to call from router."""

    def __init__(self, controller):
        self._controller = controller
        
    def __call__(self, environ, start_response):
        print("step 3: MyApplication is invoked")
        
        action_args = environ['wsgiorg.routing_args'][1].copy()
        try:
            del action_args['controller']
        except KeyError:
            pass

        try:
            del action_args['format']
        except KeyError:
            pass
        
        action = action_args.pop('action', None)
        controller_method = getattr(self._controller, action)
        result = controller_method(**action_args)
        
        start_response('200 OK', [('Content-Type', 'text/plain')])
        return [result]

class MyRouter(object):
    """Test router."""

    def __init__(self):
        route_name = "dummy_route"
        route_path = "/dummies"
        
        my_application = MyApplication(MyController()) 
        
        self.mapper = Mapper()
        self.mapper.connect(route_name, route_path,
                        controller=my_application,
                        action="getlist",
                        mykey="myvalue",
                        conditions={"method": ['GET']})
        
        
        self._router = routes.middleware.RoutesMiddleware(self._dispatch,
                                                          self.mapper)

    @webob.dec.wsgify(RequestClass=webob.Request)
    def __call__(self, req):
        """Route the incoming request to a controller based on self.map.

        If no match, return a 404.

        """
        print("step 1: MyRouter is invoked")
        return self._router

    @staticmethod
    @webob.dec.wsgify(RequestClass=webob.Request)
    def _dispatch(req):
        """Dispatch the request to the appropriate controller.

        Called by self._router after matching the incoming request to a route
        and putting the information into req.environ.  Either returns 404
        or the routed WSGI app's response.

        """
        print("step 2: RoutesMiddleware is invoked, calling our _dispatch back")
        
        match_dict = req.environ['wsgiorg.routing_args'][1]
        if not match_dict:
            return webob.exc.HTTPNotFound()
        app = match_dict['controller']
        return app
        
class RoutingTestCase(testtools.TestCase):

    def test_router(self):
        router = MyRouter()
        result = webob.Request.blank('/dummies').get_response(router)
        self.assertEqual(result.body, "getlist(), mykey=myvalue")


然后见博客中有较为详细的理解,但是要深入的话还是需要深入阅读文档

openstack基础之python WSGI,paste,Routes,webob

wsgi是在PEP333中定义的: PEP:   333 Title: Python Web Server Gateway Interface v1.0 Author: Phillip ...
  • happyAnger6
  • happyAnger6
  • 2017-01-15 23:11:20
  • 2763

Openstack源代码分析之PasteDeploy+Webob实例以及Openstack源代码下PasteDeploy+Webob+Routes分析

通过PasteDeploy+Webob来配置WSGI服务器接口 Webob是一种封装了HTTP协议的模块,具体课参考官方文档,不过这两天不知为什么不能访问,我是直接下载的源代码,源代码下docs自带本...
  • u011216699
  • u011216699
  • 2013-10-29 23:14:30
  • 7306

openstack 中的Routes + webob 的 REST API

首先贴一段代码
  • luzheqi
  • luzheqi
  • 2014-11-12 00:29:28
  • 998

深入理解openstack网络架构(4)-----连接到public network

在上一篇文章中,我们介绍了openstack中的路由,了解到openstack如何通过namespace实现的router将两个network连通。本文中,我们进一步分析路由功能,说明实现内部inte...
  • halcyonbaby
  • halcyonbaby
  • 2014-11-30 22:04:29
  • 18589

WSGI、webob、routes实例

程序代码: from wsgiref.simple_server import make_server from routes import Mapper,middleware from webob...
  • li_101357
  • li_101357
  • 2016-10-07 21:31:15
  • 579

WSGI Webob Routes 实例

转自 http://blog.csdn.net/spch2008/article/details/9004926 1.下载库文件     webob库:http://d...
  • u010545775
  • u010545775
  • 2015-03-06 17:35:05
  • 653

OpenStack工程师技术栈

0 阅读指南 希望本文能够解开你心中萦绕已久的心结,假如是死结,请移步到 https://wiki.openstack.org/wiki/Main_Page学习OpenStack其实就是学习...
  • qq_34754363
  • qq_34754363
  • 2017-05-29 08:03:43
  • 590

OpenStack

记录一个OpenStack的bug 启动实例的时候会报出500错误, 启动实例命令 nova boot --flavor 1 --key_name demo-key --image d70570...
  • caiwenguang1992
  • caiwenguang1992
  • 2016-07-15 17:26:05
  • 561

通过Rest API调用OpenStack

OpenStack本身提供两种调用的方式 一、Command Line:如nova create,nova start 等各种命令 二、Restful Webservice:供OpenStack各...
  • cnyyx
  • cnyyx
  • 2015-02-15 17:13:25
  • 5974

扩展 OpenStack 的 REST API 的方法

以Nova REST API为例, 你应该按照以下步骤来扩展 Nova REST API (使用 类nova.api.openstack.compute.contrib.floating_ips.F...
  • zhaoeryi
  • zhaoeryi
  • 2014-08-19 17:22:36
  • 4087
收藏助手
不良信息举报
您举报文章:openstack 中的Routes + webob 的 REST API
举报原因:
原因补充:

(最多只允许输入30个字)