WSGI and Paste学习笔记

原创 2016年05月31日 23:52:34

The Problem

  • Lots of web frameworks Zope, Quixote, Webware, SkunkWeb and Twisted Web etc
  • Applications written for one framework often weren't compatible with the server components of the others

HTTP Basics

  • When you request a page the browser sends an HTTP request
  • When the server receives that request it will perform some action, (typically running an application) and return an HTTP response

WSGI application

  • It is a callable (in this case a simple function) taking environ and start_response as positional parameters
  • It calls start_response() with a status code and a list of tuple pairs of headers
  • It returns a value.
  • It should only be called once.
  • The response it returns is an iterable (in this case a list with just one string).


The environ Dictionary :

A dictionary of strings

  • CGI strings
  • WSGI strings: wsgi.version, wsgi.url_scheme, wsgi.input, wsgi.errors, wsgi.multithread, wsgi.multiprocess, wsgi.run_once
  • Server extension strings

easy_install WSGIUtils




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

  • It is a callable that accepts environ and start_response
  • Calls start_repsonse once with status and headers etc
  • Returns an iterable

with mddleware, you can do the following

  • Provide more functionality by adding a key to the environ dictionary
  • Change the status
  • Intercepting an error
  • Adding/removing/changing headers
  • Change a response


Middleware Chains


Use Paste



from webob import Response

from webob.dec import wsgify

from paste import httpserver

from paste.deploy import loadapp


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='', port=8080)

  • paste.ini


paste.app_factory = app:app_factory




from webob import Response

from webob.dec import wsgify

from paste import httpserver

from paste.deploy import loadapp


def application(req):

return Response('Hello World')


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='', port=8080)

  • paste.ini


pipeline = myfilter myapp


paste.app_factory = app:app_factory


paste.filter_factory = app:filter_factory


Paste Deploy

  • Paste Deployment is a system for finding and configuring WSGI applications and servers.
  • For WSGI application consumers:
    • it provides a single, simple function (loadapp) for loading a WSGI application from a configuration file or a Python Egg.
  • For WSGI application providers
    • it only asks for a single, simple entry point to your application
  • two URI formats currently supported:
  • Global and Local Configurations
    • Global configuration to apply to every application defined in a file should go in a special section named [DEFAULT].


admin_email =

    • Configuration is done through keys besides use


use = egg:MyBlog

database = mysql://localhost/blogdb

blogname = This Is My Blog!


  • Tree types of sections:
    • Applications
    • Composite Applications
    • Filter Composition
  • Application section: There’s two ways to indicate the Python code for the application.
    • The first is to refer to another URI or name:

#points to application section in other config files


use = config:another_config_file.ini#app_name

# or any URI:


use = egg:MyApp

# or a callable from a module:


use = call:my.project:myapplication

# or even another section:


use = myotherapp

    • The other way to define an application is to point exactly to some Python code:


paste.app_factory = myapp.modulename:app_factory


  • Composite Applications
    • “Composite” applications are things that act like applications, but are made up of other applications.
    • One example would be a URL mapper, where you mount applications at different URL paths.


use = egg:Paste#urlmap

/ = mainapp

/files = staticapp


use = egg:MyApp


use = egg:Paste#static

document_root = /path/to/docroot

  • Filter Composition: several ways to apply filters to applications:
    • The first way is to use the filter-with setting


use = egg:MyEgg

filter-with = printdebug


use = egg:Paste#printdebug

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

    • filter-app defines a filter, and then a special key next which points to the application to apply the filter to.


use = egg:Paste#urlmap

/ = home

/blog = blog

/wiki = wiki

/cms = config:cms.ini


use = egg:Paste#static

document_root = %(here)s/htdocs


use = egg:Authentication#auth

next = blogapp

roles = admin

htpasswd = /home/me/users.htpasswd


use = egg:BlogApp

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


use = call:mywiki.main:application

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

    • pipeline: is used when you need apply a number of filters.


pipeline = filter1 egg:FilterEgg#filter2 filter3 app




  • Factories
    • paste.app_factory
    • paste.composite_factory
    • paste.filter_factory
    • paste.server_factory


  • keystoneclient/middleware/

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()


    def auth_filter(app):

        return AuthProtocol(app, conf)

    return auth_filter


def app_factory(global_conf, **local_conf):

    conf = global_conf.copy()


    return AuthProtocol(None, conf)



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)


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
  • 2759


  • mygrus
  • mygrus
  • 2017-03-09 16:30:48
  • 454

python WSGI+Paste Deployment+oslo.config综合运用

关于Paste Deployment介绍请看我之前的博客Python Paste Deployment 关于oslo.config介绍请看我之前博客oslo.config 关于WSGI介绍请看我之前博...
  • shuaijiasanshao
  • shuaijiasanshao
  • 2016-04-23 09:43:06
  • 3211


对Restful API有了一个基础的了解,那么我们来看通过URL是怎样映射到具体的应用程序操作函数上了。在OpenStack中的API Daemon都会有一个Router类,来构建资源与URL直接的...
  • tantexian
  • tantexian
  • 2014-07-13 16:39:12
  • 9227

【OpenStack】WSGI and Webob+Paste示例

声明: 本博客欢迎转发,但请保留原作者信息! 新浪微博:@孔令贤HW; 博客地址: 内容系本人学习、研究和总结,如有雷同,实属荣...
  • lynn_kong
  • lynn_kong
  • 2013-04-18 12:40:54
  • 3406

11.2.2、搭建RESTful API之使用PasteDeploy定制WSGI服务

WSGI服务的相关相关基础只是前面已经介绍,而且Paste模块也有所介绍。现在就一步一步实现paste模块的每个功能。 一、知识点讲解 1、为什么需要 PasteDeploy 模块? 一个 WSG...
  • li_101357
  • li_101357
  • 2016-10-10 23:36:14
  • 277

探索 OpenStack 之(11):cinder-api Service 启动过程分析 以及 WSGI / Paste deploy / Router 等介绍

转自:探索 OpenStack 之(11):cinder-api Service 启动过程分析 以及 WSGI / Paste deploy / Router 等介绍       Open...
  • jmppok
  • jmppok
  • 2015-08-17 14:11:33
  • 845

Benchmark of Python WSGI Servers

Benchmark of Python WSGI Servers Nicholas Piël | March 15, 2010 It has been a while since the ...
  • 2014-05-01 22:48:33
  • 1371


  • tantexian
  • tantexian
  • 2014-11-27 15:57:33
  • 3067

python paste介绍和使用教程

paste定义了一些列wsgi开发的库, paste.deploy定义了配置wsgi应用程序的ini文件格式和对应的解析库,此外还提供了一个简单的函数(loadapp),用于从ini格式的...
  • weiyuanke
  • weiyuanke
  • 2013-12-03 11:01:50
  • 2517
您举报文章:WSGI and Paste学习笔记