Cherrypy虚拟主机 - 单独的应用程序配置

时间:2014-10-24 10:54:40

标签: python configuration virtualhost cherrypy

我想在同一个IP上的不同域上放置两个樱桃应用程序。如何使用cherrypy virtualhost调度程序为这些站点提供单独的配置文件,而不是隐藏Apache背后的网络服务器?

import cherrypy
from cherrypy import expose

class Root(object):
    @expose
    def index(self):
        return "The Main Page" + \
          '<br /><a href="/rs/file.txt">File from the Main app</a>'

class SecondApp(object):
    @expose
    def index(self):
        return "Page on Subdomain" + \
          '<br /><a href="/rs/file.txt">Another file from the subdomain app</a>'

def main():
    cherrypy.config.update('global.conf')

    conf = {"/": {"request.dispatch": cherrypy.dispatch.VirtualHost(
                    **{ "sub.domain.local": "/secondapp" } )
                 }
           }

    root = Root()
    root.secondapp = SecondApp()
    app=cherrypy.tree.mount(root, "/", conf)
    app.merge('some.additional.configuration')
    cherrypy.engine.start()
    cherrypy.engine.block()

if __name__ == "__main__": main()

'conf'词汇表定义的配置对我的域'domain.local'和'sub.domain.local'都有影响。 如何在此处为应用程序“secondapp”插入单独的配置?

1 个答案:

答案 0 :(得分:0)

您需要稍微更改主要内容。您必须添加另一个配置部分,然后使用cherrypy.tree.mount将其与conf对象一起添加。

def main():
    conf = {"/": {"request.dispatch": cherrypy.dispatch.VirtualHost(
                    **{ "sub.domain.local": "/secondapp" } )
                 }
           }
    secondapp_conf = {
        ... Config Items for Second App ....
        }
    }

    root = Root()
    root.secondapp = SecondApp()
    cherrypy.tree.mount(root, "/", conf)
    cherrypy.tree.mount(root.secondapp, "/secondapp", secondapp_conf)
    cherrypy.engine.start()
    cherrypy.engine.block()