paste deploy python ini 配置文件容易说明


        paste deploy python ini 配置文件简单说明<br />

配合 <br />


看下面的example(从网络上改造而来,出处URL也搞忘记的),很快会对python的paste如何组织wsgi有一个简单的了解,

pastedeploylab.ini

  [DEFAULT] key1=value1 key2=value2 key3=values [composite:pdl] use=egg:Paste#urlmap /:root /calc:calc [filter:logrequest] username = root password = root123 paste.filter_factory = pastedeploylab:LogFilter.factory [filter:testrequest] username = admin password = admin paste.filter_factory = pastedeploylab:TestFilter.factory [app:calculator] description = This is an "+-*/" Calculator paste.app_factory = pastedeploylab:Calculator.factory [app:showversion] version = 1.0.0 paste.app_factory = pastedeploylab:ShowVersion.factory [pipeline:root] pipeline = logrequest testrequest showversion [pipeline:calc] pipeline = logrequest calculator



pastedeployed.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 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 TestFilter(): def __init__(self,app): self.app = app def __call__(self,environ,start_response): print "filter:TestFilter is called." return self.app(environ,start_response) @classmethod def factory(cls, global_conf, **kwargs): print "in TestFilter.factory", global_conf, kwargs return TestFilter 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 print "in ShowVersion.factory:" print "global_conf=", global_conf print "kwargs =", 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',8989,wsgi_app)

  server.serve_forever()

相关链接

perl 程序开发 Perl语言基础

kasumi2 10 years, 6 months ago

Your Answer