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

image 

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

image

 

Middleware

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

image

Middleware Chains

image

Use Paste

例子一

  • app.py

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)

  • paste.ini

[app:main]

paste.app_factory = app:app_factory

 

例子二

  • app.py

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)

  • paste.ini

[pipeline:main]

pipeline = myfilter myapp

[app:myapp]

paste.app_factory = app:app_factory

[filter:myfilter]

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].

[DEFAULT]

admin_email = webmaster@example.com

    • Configuration is done through keys besides use

[app:blog]

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

[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

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

[app:myapp]

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.

[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

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

[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...

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

[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: is used when you need apply a number of filters.

[pipeline:main]

pipeline = filter1 egg:FilterEgg#filter2 filter3 app

[filter:filter1]

...

 

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

 

  • keystoneclient/middleware/auth_token.py

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://blog.csdn.net/popsuper1982/article/details/51550368

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

学习Python:WSGI_02_使用Paste库

这里我们来结合Python中的Paste库,继续进行说明。如果我们想对不同的请求路径,让不同的应用程序去处理,这该怎么办?一个常见的解决办法就是,我们可以很方便的使用Paste库来实现我们的想法。...
  • 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

openstack_ice之wsgi详解(paste从ini配置文件->routesr具体发布流程)

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

【OpenStack】WSGI and Webob+Paste示例

声明: 本博客欢迎转发,但请保留原作者信息! 新浪微博:@孔令贤HW; 博客地址:http://blog.csdn.net/lynn_kong 内容系本人学习、研究和总结,如有雷同,实属荣...
  • 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 ...
  • CPP_CHEN
  • CPP_CHEN
  • 2014-05-01 22:48:33
  • 1371

仿Openstack的WSGI接口及RESTul服务实现(python)(转)

Openstack的WSGI接口通过webob,pastedeploy,routes实现了Controller类,和Router类,这里仿照Openstack的WSG接口实现简单的测试程序首先是tes...
  • 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学习笔记
举报原因:
原因补充:

(最多只允许输入30个字)