CherryPy Project Download

How to do Virtual Hosting

CHerryPy has a built-in dispatcher for supporting Virtual Hosts. See:

Put simply this means you can develop an application that can serve different parts of itself for different virtual hosts. For example, say you have an application mounted at / on http://domain.com/ you could map:

Example

conf.ini

[global]
server.socket_port = 8000
server.thread_pool = 10

vhosts.py

#!/usr/bin/env python

import cherrypy
from cherrypy import expose

class Root(object):

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

class Foo(object):

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

class Bar(object):

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

def main():
        cherrypy.config.update("conf.ini")

        conf = {
                        "/": {
                                "request.dispatch": cherrypy.dispatch.VirtualHos
t(
                                        **{
                                                "foo.domain.com:8000": "/foo",
                                                "bar.domain.com:8000": "/bar"
                                                }
                                        )
                                }
                        }

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

        cherrypy.server.quickstart()
        cherrypy.engine.start()

if __name__ == "__main__":
        main()

Attachments

Hosted by WebFaction

Log in as guest/cherrypy to create/edit wiki pages