python - Cherrypy 3.2 虚拟主机调度程序

标签 python python-3.x cherrypy virtualhost

我正在尝试让虚拟主机在 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,我得到的唯一页面是 Root。

有人可以帮帮我吗?

最佳答案

他们在 cherrypy 文档中显示的端口值不是“80”。 curl 至少,如果端口为80,则不在Host 请求头中添加端口号;我怀疑 cherrypy.dispatch.VirtualHost 不够聪明,无法将端口 80 上的 example.com 的主机 header 匹配到 example.com:80 反之亦然。我可能会在配置中映射两个主机(有和没有端口号),以防不寻常的主机头碰巧从线路上掉下来。

关于python - Cherrypy 3.2 虚拟主机调度程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6521126/

相关文章:

python - 元组列表的唯一排列

python - 多个文件中的单词匹配

python - 为什么 tkinter 比例小部件需要 tkinter 变量

python - CherryPy 重定向到 root

javascript - 模态框没有完全显示

python - 规范化餐厅菜肴列表

python - OpenCV,如何加入/填充线或框或圆中的间隙(Python)

python - Matplotlib 的 "interactive mode"(ion(), ioff()) 的确切语义?

python - Tkinter - Python 3.5

google-app-engine - 如何在 Google App Engine 中运行 CherryPy 网络服务器