设为首页 收藏本站
查看: 1866|回复: 0

[经验分享] 大话openstack之wsgi&webob.dec.wsgify

[复制链接]

尚未签到

发表于 2016-1-9 09:56:58 | 显示全部楼层 |阅读模式
  
  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.iyunv.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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-162089-1-1.html 上篇帖子: 结合nova-network分析openstack mq机制 下篇帖子: OpenStack多节点部署(二)——操作系统安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表