【OpenStack】WSGI and Webob+Paste示例

声明:
本博客欢迎转发,但请保留原作者信息!
新浪微博:@孔令贤HW;
博客地址:http://blog.csdn.net/lynn_kong

内容系本人学习、研究和总结,如有雷同,实属荣幸!

1. 一个简单的WSGI程序

"""Hello World using WSGI """
from paste import httpserver
def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/html')])
    return ['Hello World']
httpserver.serve(application, host='127.0.0.1', port=8080)
这个不用过多解释,wsgi的入门程序。在本机用cURL工具可以进行测试。

2. WSGI With WebOb + Paste

"""Hello World using WebOb, Paste + WSGI """
from webob import Response
from webob.dec import wsgify
from webob import exc
from paste import httpserver
from paste.deploy import loadapp

INI_PATH = '/home/paste.ini'

@wsgify
def application(request):
    return Response('Hello World of WebOb!')

@wsgify.middleware
def auth_filter(request, app):
    if request.headers.get('X-Auth-Token') != 'konglingxian':
        return exc.HTTPForbidden()
    return app(request)
    
def app_factory(global_config, **local_config):
    return application
    
def filter_factory(global_config, **local_config):
    return auth_filter
    
wsgi_app = loadapp('config:' + INI_PATH)
httpserver.serve(wsgi_app, host='127.0.0.1', port=8080)
在paste.ini中的配置如下:
[pipeline:main]
pipeline = auth hello
[app:hello]
paste.app_factory = wsgi_webob_test:app_factory
[filter:auth]
paste.filter_factory = wsgi_webob_test:filter_factory




你可能感兴趣的:(openstack,paste,wsgi,webob)