Source

CherryPy Recipes / deployment / vhost_wsgi / __init__.py

Full commit
# -*- coding: utf-8 -*-
""" CherryPy supports virtual hosting
of WSGI applications using ``cherrypy._cpwsgi.VirtualHost``
which takes a default WSGI app and a mapping
of domains to WSGI apps.

To run this example, add the following lines to your
hosts file:

127.0.0.1       my.example.com
127.0.0.1       www.example.com

Then start the server and go to:

http://my.example.com:8090/
http://www.example.com:8090/
"""

def default(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world!']

def app1(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world from app1!']

def app2(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    start_response(status, response_headers)
    return ['Hello world from app2!']

if __name__ == '__main__':
    import cherrypy
    cherrypy.config.update({'server.socket_port': 8090})
    
    domains = {
        'my.example.com:8090': app1,
        'www.example.com:8090': app2
    }
        
    cherrypy.tree.graft(cherrypy._cpwsgi.VirtualHost(default, domains))
    cherrypy.engine.start()
    cherrypy.engine.block()