站点图标 起风网

WSGI and Paste学习笔记

The Problem

HTTP Basics

WSGI application

 

The environ Dictionary :

A dictionary of strings

easy_install WSGIUtils

 

Middleware

Component that acts like an application from the server’s point of view

with mddleware, you can do the following

Middleware Chains

Use Paste

例子一

from webob import Response

from webob.dec import wsgify

from paste import httpserver

from paste.deploy import loadapp

@wsgify

def application(req):

return Response(‘Hello World’)

def app_factory(global_config, **local_config):

return application

wsgi_app = loadapp(‘config:/root/paste.ini’)

httpserver.serve(wsgi_app, host=’127.0.0.1′, port=8080)

[app:main]

paste.app_factory = app:app_factory

 

例子二

from webob import Response

from webob.dec import wsgify

from paste import httpserver

from paste.deploy import loadapp

@wsgify

def application(req):

return Response(‘Hello World’)

@wsgify.middleware()

def my_filter(req, app):

# just print a message to the console

print(‘my_filter was called’)

return app(req)

def app_factory(global_config, **local_config):

return application

def filter_factory(global_config, **local_config):

return my_filter

wsgi_app = loadapp(‘config:/root/paste.ini’)

httpserver.serve(wsgi_app, host=’127.0.0.1′, port=8080)

[pipeline:main]

pipeline = myfilter myapp

[app:myapp]

paste.app_factory = app:app_factory

[filter:myfilter]

paste.filter_factory = app:filter_factory

 

Paste Deploy

[DEFAULT]

admin_email = webmaster@example.com

[app:blog]

use = egg:MyBlog

database = mysql://localhost/blogdb

blogname = This Is My Blog!

 

#points to application section in other config files

[app:myapp]

use = config:another_config_file.ini#app_name

# or any URI:

[app:myotherapp]

use = egg:MyApp

# or a callable from a module:

[app:mythirdapp]

use = call:my.project:myapplication

# or even another section:

[app:mylastapp]

use = myotherapp

[app:myapp]

paste.app_factory = myapp.modulename:app_factory

 

[composite:main]

use = egg:Paste#urlmap

/ = mainapp

/files = staticapp

[app:mainapp]

use = egg:MyApp

[app:staticapp]

use = egg:Paste#static

document_root = /path/to/docroot

[app:main]

use = egg:MyEgg

filter-with = printdebug

[filter:printdebug]

use = egg:Paste#printdebug

# and you could have another filter-with here, and so on…

[composite:main]

use = egg:Paste#urlmap

/ = home

/blog = blog

/wiki = wiki

/cms = config:cms.ini

[app:home]

use = egg:Paste#static

document_root = %(here)s/htdocs

[filter-app:blog]

use = egg:Authentication#auth

next = blogapp

roles = admin

htpasswd = /home/me/users.htpasswd

[app:blogapp]

use = egg:BlogApp

database = sqlite:/home/me/blog.db

[app:wiki]

use = call:mywiki.main:application

database = sqlite:/home/me/wiki.db

[pipeline:main]

pipeline = filter1 egg:FilterEgg#filter2 filter3 app

[filter:filter1]

 

 

class AuthProtocol(object):

    “””Auth Middleware that handles authenticating client calls.”””

    def __init__(self, app, conf):

        ……

    def __call__(self, env, start_response):

        “””Handle incoming request.

        Authenticate send downstream on success. Reject request if we can’t authenticate.

 

def filter_factory(global_conf, **local_conf):

    “””Returns a WSGI filter app for use with paste.deploy.”””

    conf = global_conf.copy()

    conf.update(local_conf)

    def auth_filter(app):

        return AuthProtocol(app, conf)

    return auth_filter

 

def app_factory(global_conf, **local_conf):

    conf = global_conf.copy()

    conf.update(local_conf)

    return AuthProtocol(None, conf)

 

[composite:rootapp]

paste.composite_factory = glance.api:root_app_factory

/: apiversions

/v1: apiv1app

/v2: apiv2app

 

def root_app_factory(loader, global_conf, **local_conf):

    if not CONF.enable_v1_api:

        del local_conf[‘/v1’]

    if not CONF.enable_v2_api:

        del local_conf[‘/v2’]

    return paste.urlmap.urlmap_factory(loader, global_conf, **local_conf)

文章转载于:https://www.cnblogs.com/popsuper1982/p/3800430.html

原著是一个有趣的人,若有侵权,请通知删除

退出移动版