大话openstack之wsgi&webob.dec.wsgify

Webob中针对WSGI的装饰器,这应该是比较重要的部分了。 Webob为WSGI主要提高了一个装饰器wsgify,作用就是将一个函数转换成一个WSGI应用
wsgify
class webob.dec.wsgify(func=None, RequestClass=None, args=(), kwargs=None, middleware_wraps=None) 将一个request作为输入,response作为输出的函数wrap包装成一个WSGI应用。用法如下:
@wsgify
def myfunc(req):
    return webob.Response('hey there')
经过装饰后,函数myfunc作为一个WSGI应用,有两种调用方式:
app_iter = myfunc(environ, start_response)
resp = myfunc(req)
如果处理中触发了webob.exc异常,异常信息会传入Response中。
装饰器还可以自定义,一般通过一个subrequest来实现,如下:
class MyRequest(webob.Request):
    @property
    def is_local(self):
        return self.remote_addr == '127.0.0.1'
@wsgify(RequestClass=MyRequest)
def myfunc(req):
    if req.is_local:
        return Response('hi!')
    else:
        raise webob.exc.HTTPForbidden
可以看到,经过这种方式,我们在整个WSGI中增加了request中一些自定义内容的处理。另外一种自定义方式就是给装饰器wsgify加入参数支持,从而一个函数就可以创建多个WSGI应用。如下:
import simplejson
def serve_json(req, json_obj):
    return Response(json.dumps(json_obj),
                    content_type='application/json')

serve_ob1 = wsgify(serve_json, args=(ob1,))
serve_ob2 = wsgify(serve_json, args=(ob2,))
使用装饰器后,一个函数可以返回如下几种信息:
webob.Response对象或者sub对象
任意WSGI应用
None(将会使用req.response)
一个字符串,会被写到req.response中
从webob.exc中触发异常


webob.dec – WSGIfy decorator
Decorators to wrap functions to make them WSGI applications.

The main decorator wsgify turns a function into a WSGI application (while also allowing normal calling of the method with an instantiated request).

Decorator
class webob.dec.wsgify(func=None, RequestClass=None, args=(), kwargs=None, middleware_wraps=None)
Turns a request-taking, response-returning function into a WSGI app

You can use this like:

@wsgify
def myfunc(req):
    return webob.Response('hey there')
With that myfunc will be a WSGI application, callable like app_iter = myfunc(environ, start_response). You can also call it like normal, e.g., resp = myfunc(req). (You can also wrap methods, like def myfunc(self, req).)

If you raise exceptions from webob.exc they will be turned into WSGI responses.

There are also several parameters you can use to customize the decorator. Most notably, you can use a webob.Request subclass, like:

class MyRequest(webob.Request):
    @property
    def is_local(self):
        return self.remote_addr == '127.0.0.1'
@wsgify(RequestClass=MyRequest)
def myfunc(req):
    if req.is_local:
        return Response('hi!')
    else:
        raise webob.exc.HTTPForbidden
Another customization you can add is to add args (positional arguments) or kwargs (of course, keyword arguments). While generally not that useful, you can use this to create multiple WSGI apps from one function, like:

import simplejson
def serve_json(req, json_obj):
    return Response(json.dumps(json_obj),
                    content_type='application/json')

serve_ob1 = wsgify(serve_json, args=(ob1,))
serve_ob2 = wsgify(serve_json, args=(ob2,))
You can return several things from a function:

A webob.Response object (or subclass)
Any WSGI application
None, and then req.response will be used (a pre-instantiated Response object)
A string, which will be written to req.response and then that response will be used.
Raise an exception from webob.exc

 参考链接

【VIP】Python.Paste指南之WebOb
http://blog.csdn.net/ztejiagn/article/details/8722838

http://docs.webob.org/en/latest/reference.html#introduction

【vip】webob.dec – WSGIfy decorator
http://docs.webob.org/en/latest/api/dec.html?highlight=wsgify#webob.dec.wsgify

【vip】WSGI初探
http://linluxiang.iteye.com/blog/799163
http://blog.linluxiang.info/2011/03/03/wsgi-learn/

Webob WSGI 装饰器

http://blog.csdn.net/spch2008/article/details/9003410

http://blog.csdn.net/ddl007/article/details/8602731


  @webob.dec.wsgify(RequestClass=Request)

   方法 __call__   WSGI服务请求调用接口,通过装饰后实际上是将此处__call__方法的返回值作为参数传入到webob.dec.wsgify装饰类,并调用该类的同名方法__call__, __call__方法又根据web请求调用app既Resource的__call__来完成具体的功能,每当有nova-api相关请求时均会进入到Resource的__call__方法以便进行请求的dispatch,所以下面的ExtensionsResource就主要是将各个功能类进行manage操作,以便完成服务请求。此处需要注意的是设计模式-Decorator(装饰者模式)。

http://www.aboutyun.com/thread-8551-1-1.html


http://www.aboutyun.com/thread-8551-1-1.html


http://blog.csdn.net/tantexian/article/details/41547651

WebOb的简单介绍 – bingotree

http://bingotree.cn/?p=109

仿Openstack的WSGI接口及RESTul服务实现(python)(转)
http://blog.csdn.net/tantexian/article/details/41547651


使用Python Routes + webob 的 REST API的测试程序._Python_其它语言-ITnose
http://www.itnose.net/detail/6098529.html

openstack-wsgi-restful
http://blog.csdn.net/tantexian/article/category/2393281


python Route 简单使用笔记
http://mathslinux.org/?p=598

猜你喜欢

转载自chenyingkof.iteye.com/blog/2249439
DEC