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)

版权声明:本文为博主原创文章,未经博主允许不得转载。

学习Python:WSGI_02_使用Paste库

这里我们来结合Python中的Paste库,继续进行说明。如果我们想对不同的请求路径,让不同的应用程序去处理,这该怎么办?一个常见的解决办法就是,我们可以很方便的使用Paste库来实现我们的想法。...
  • mygrus
  • mygrus
  • 2017年03月09日 16:30
  • 297

paste.deploy学习笔记

http://blog.lightcloud.cn/?p=183 Pasted Deployment是一套查找和配置WSGI应用和服务的系统。向WSGI application(WSGI应用...

Python Paste 学习笔记

一、写在前面这篇文章主要介绍了Python的Paste库的使用,学习过程中主要参考官网文档以及自己的理解,整理成笔记以便后续自己查阅。 如果转载,请保留作者信息。 邮箱地址:jpzhang.h...

web.py学习笔记(一)Apache+WSGI运行web

1、下载WSGI模块,放在Apache的modules下面。http://code.google.com/p/modwsgi/2、Apache下配置WSGILoadModule wsgi_module...
  • treesky
  • treesky
  • 2011年05月16日 12:30
  • 1718

wsgi+webob+routes学习笔记-初学篇(中)

接上文: 可以看到wsgi已经在8090端口上建立了,用Rest client发个消息看下,可以看到已经收到了响应“Hello, World! I am sxmatch” 在以上程序中:方法d...
  • sxmatch
  • sxmatch
  • 2013年08月08日 10:47
  • 785

web.py——WSGI学习笔记

在web项目部署时,很好奇web服务器是如何通过简单的配置,兼容多种web框架甚至多种编程语言。经过一些了解知道了CGI/WSGI等概念,鉴于对python比较熟悉,在这里对我认识到的WSGI做一个简...
  • liyh_xd
  • liyh_xd
  • 2017年12月03日 19:01
  • 17

openstack基础之python WSGI,paste,Routes,webob

wsgi是在PEP333中定义的: PEP:   333 Title: Python Web Server Gateway Interface v1.0 Author: Phillip ...

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

感谢朋友支持本博客,欢迎共同探讨交流,由于能力和时间有限,错误之处在所难免,欢迎指正! 如有转载,请保留源作者博客信息。 Better Me的博客:blog.csdn....

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

对Restful API有了一个基础的了解,那么我们来看通过URL是怎样映射到具体的应用程序操作函数上了。在OpenStack中的API Daemon都会有一个Router类,来构建资源与URL直接的...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:WSGI and Paste学习笔记
举报原因:
原因补充:

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