Cherrypy 3.2 虚拟主机调度器

3 投票
1 回答
1067 浏览
提问于 2025-04-16 20:31

我正在尝试在使用Python 3的CherryPy 3.2.0中设置虚拟主机:

#!/usr/bin/env python

import cherrypy
from cherrypy import expose

class Root(object):

    @expose
    def index(self):
        return "I am the root  vhost"

class Foo(object):

    @expose
    def index(self):
        return "I am testingdomain.com"

class Bar(object):

    @expose
    def index(self):
        return "I am testingdomain2.com."

def main():

    cherrypy.config.update({'server.socket_host': 'rootdomain.com',
            'server.socket_port': 80,
    })

    conf = {
        "/": {
            "request.dispatch": cherrypy.dispatch.VirtualHost(
            **{
                "testingdomain.com:8000": "/foo",
                "testingdomain2.com:8000": "/bar"
            })
        }
    }

    root = Root()
    root.foo = Foo()
    root.bar = Bar()
    cherrypy.tree.mount(root, "", conf)

    #cherrypy.quickstart()
    cherrypy.engine.start()
    cherrypy.engine.block()

if __name__ == "__main__":
    main()

我在/etc/hosts文件中列出了测试域名。当我请求这些域名时,它们能正确指向服务器。但是,无论我访问testingdomain.com还是testingdomain2.com,得到的页面都是根目录的内容。

有人能帮帮我吗?

1 个回答

2

在cherrypy的文档中,他们提到的端口号不是'80'。至少在使用curl的时候,如果端口是80,它不会在Host请求头中添加端口号。我怀疑cherrypy.dispatch.VirtualHost没有足够聪明,不能把端口为80的example.comexample.com:80匹配起来,反之亦然。我可能会在配置中同时映射这两种主机(有端口号和没有端口号的),以防出现一些不寻常的主机头信息。

撰写回答