本文地址:http://blog.youkuaiyun.com/spch2008/article/details/9005885
Paste环境准备
1. 下载paste,放于eclipse目录中
paste库:http://download.youkuaiyun.com/detail/spch2008/5500979
2.目录结构
现在将之前的程序改写,改用Paste进行部署。
LoadApp
'''
Created on 2013-6-2
@author: spch2008
'''
from wsgiref.simple_server import make_server
import routes.middleware
import webob.dec
import webob.exc
from paste.deploy import loadapp
class Controller:
@webob.dec.wsgify
def __call__(self, req):
return webob.Response("Hello World!")
class Router(object):
def __init__(self):
self._mapper = routes.Mapper()
self._mapper.connect('/spch',
controller=Controller(),
action='index',
conditions={'method': ['GET']})
self._router = routes.middleware.RoutesMiddleware(self._dispatch, self._mapper)
@classmethod
def app_factory(cls, global_config, **local_config):
return cls()
@webob.dec.wsgify
def __call__(self, req):
return self._router
@staticmethod
@webob.dec.wsgify
def _dispatch(req):
match = req.environ['wsgiorg.routing_args'][1]
if not match:
return webob.exc.HTTPNotFound()
app = match['controller']
return app
if __name__ == "__main__":
app = loadapp('config:C:/paste.ini', name='hello')
httpd = make_server('localhost', 8282, app)
httpd.serve_forever()
需要在类中添加一个app_factory方法,然后通过loadapp获得实例对象。
paste.ini放于C盘中,文件内容如下:
[app:hello]
paste.app_factory = MyRoute:Router.app_factory
Composite Applications
def factory(loader, global_conf, **local_conf):
print (local_conf)
return Router()
if __name__ == "__main__":
app = loadapp('config:D:/paste.ini', name='hello')
httpd = make_server('localhost', 8282, app)
httpd.serve_forever()
paste.ini配置内容为
[composite:hello]
use = call:MyRoute:factory
date = 2013
author = spch2008
local_conf输出:{'date': '2013', 'author': 'spch2008'}
另一个例子,但此例没调试成,因为可能由于windows没有egg概念吧(猜想!)
[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
main使用Paste的urlmap程序,进行url匹配。
匹配"/"转向mainapp段;“/files”转向staticapp段。
Filter Composition
[app:main]
use = egg:MyEgg
filter-with = printdebug
[filter:printdebug]
use = egg:Paste#printdebug
可以使用pipeline进行filter
[pipeline:main]
pipeline = filter1 filter2 filter3 app
[filter:filter1]