Paste.Deploy主要是用来载入WSGI中的Web App使用,其核心函数是loadapp(),通过loadapp函数和一个配置文件或者egg包来载入WSGI应用。
一个配置文件后缀为ini,内容被分为很多段(section),PD只关心带有前缀的段,比如[app:main]或者[filter:errors],总的来说,一个section的标识就是[type:name],不是这种类型的section将会被忽略。
一个section的内容是以键=值来标示的。#是一个注释。在段的定义中,有以下几类:
[app:main]:定义WSGI应用,main表示只有一个应用,有多个应用的话main改为应用名字
[server:main]:定义WSGI的一个server。
[composite:xxx]:表示需要将一个请求调度定向(dispatched)到多个,或者多种应用上,compostion是复合的意思。
[fliter:xxx]:定义“过滤器”,将应用进行进一步的封装。
[DEFAULT]:定义一些默认变量的值。
[filter-app:xxx]:定义一个filter,并且使用一个特殊的关键字next指向application,将application添加到filter中(这与[filter:…]很类似)
[pipeline:xxx]:当需要定义一系列的fiter时应用。在configuration中添加关键字pipeline,pipeline是一个filters的链表,以一个application结束,例如
[pipeline:main]
pipeline = filter1 egg:FilterEgg#filter2 filter3 app
python paste.deploy中filter,filter_factory,app,app_factory的规范在文档中都没怎么写清楚,我来给你补上吧:
1.app是一个callable object,接受的参数(environ,start_response),这是paste系统交给application的,符合WSGI规范的参数. app需要完成的任务是响应envrion中的请求,准备好响应头和消息体,然后交给start_response处理,并返回响应消息体。
2.filter是一个callable object,其唯一参数是(app),这是WSGI的application对象,见(1),filter需要完成的工作是将application包装成另一个application(“过滤”),并返回这个包装后的application。
3.app_factory是一个callable object,其接受的参数是一些关于application的配置信息:(global_conf,**kwargs),global_conf是在ini文件中default section中定义的一系列key-value对,而**kwargs,即一些本地配置,是在ini文件中,app:xxx section中定义的一系列key-value对。app_factory返回值是一个application对象。
4.filter_factory是一个callable object,其接受的参数是一系列关于filter的配置信息:(global_conf,**kwargs),global_conf是在ini文件中default section中定义的一系列key-value对,而**kwargs,即一些本地配置,是在ini文件中,filter:xxx section中定义的一系列key-value对。filter_factory返回一个filter对象。
应用的定义支持以下五种格式:
use = config:another_config_file.ini#app_name
#使用另外一个配置文件
use = egg:MyApp
#使用egg包中的内容
use = call:my.project:myapplication
#使用模块中的callable对象
use = myotherapp
#使用另外一个section
paste.app_factory = myapp.modulename:app_factory
#使用工厂函数
例子:
pastedeploylab.ini:
[DEFAULT]
key1=value1
key2=value2
key3=values
[composite:pdl]
use=egg:Paste#urlmap
/:root
/calc:calc
[pipeline:root]
pipeline = logrequest showversion
[pipeline:calc]
pipeline = logrequest calculator
[filter:logrequest]
username = root
password = root123
paste.filter_factory = pastedeploylab:LogFilter.factory
[app:showversion]
version = 1.0.0
paste.app_factory = pastedeploylab:ShowVersion.factory
[app:calculator]
description = This is an "+-*/" Calculator
paste.app_factory = pastedeploylab:Calculator.factory
pastedeploylab.py
import os
import webob
from webob import Request
from webob import Response
from paste.deploy import loadapp
from wsgiref.simple_server import make_server
#Filter
class LogFilter():
def __init__(self,app):
self.app = app
pass
def __call__(self,environ,start_response):
print "filter:LogFilter is called."
return self.app(environ,start_response)
@classmethod
def factory(cls, global_conf, **kwargs):
print "in LogFilter.factory", global_conf, kwargs
return LogFilter
class ShowVersion():
def __init__(self):
pass
def __call__(self,environ,start_response):
start_response("200 OK",[("Content-type", "text/plain")])
return ["Paste Deploy LAB: Version = 1.0.0",]
@classmethod
def factory(cls,global_conf,**kwargs):
print "in ShowVersion.factory", global_conf, kwargs
return ShowVersion()
class Calculator():
def __init__(self):
pass
def __call__(self,environ,start_response):
req = Request(environ)
res = Response()
res.status = "200 OK"
res.content_type = "text/plain"
# get operands
operator = req.GET.get("operator", None)
operand1 = req.GET.get("operand1", None)
operand2 = req.GET.get("operand2", None)
print req.GET
opnd1 = int(operand1)
opnd2 = int(operand2)
if operator == u'plus':
opnd1 = opnd1 + opnd2
elif operator == u'minus':
opnd1 = opnd1 - opnd2
elif operator == u'star':
opnd1 = opnd1 * opnd2
elif operator == u'slash':
opnd1 = opnd1 / opnd2
res.body = "%s /nRESULT= %d" % (str(req.GET) , opnd1)
return res(environ,start_response)
@classmethod
def factory(cls,global_conf,**kwargs):
print "in Calculator.factory", global_conf, kwargs
return Calculator()
if __name__ == '__main__':
configfile="pastedeploylab.ini"
appname="pdl"
wsgi_app = loadapp("config:%s" % os.path.abspath(configfile), appname)
server = make_server('localhost',8080,wsgi_app)
server.serve_forever()
pass
使用:
http://127.0.0.1:8080/
输出:
Paste Deploy LAB: Version = 1.0.0
http://127.0.0.1:8080/calc?operator=plus&operand1=12&operand2=23
输出:
UnicodeMultiDict([('operator', u'plus'), ('operand1', u'12'), ('operand2', u'23')])
RESULT= 35
原文地址:http://blog.csdn.net/sonicatnoc/article/details/6539716