This page discusses CherryPy 3.1. For the slightly more complicated way to do this in 3.0, see DeployTemplate30.
Deployment Template
When developing a CherryPy 3 application that sits behind some other server (like mod_python), it can often be difficult to get error messages. If you start writing your project using this template, you'll have an easier time pinpointing and debugging errors.
This template allows you to develop and debug your application in stages:
- Use the builtin server first by running this script from the command line. All errors are then output to the console, and you can easily see startup errors that occur before logging can be set up. This includes SyntaxErrors that you would otherwise have to discover in the Apache error log.
- If needed, use no server at all by importing this module and then calling serverless(). This allows you to ask the Engine for a Request object and manipulate it directly (even step through the request process with pdb if you like).
- When everything works using the builtin server, you can tell Apache to serve your application via mod_python by the following lines:
<Location />
SetHandler python-program
PythonHandler cherrypy._cpmodpy::handler
PythonDebug On
</Location>
Here is the script:
"""Deployment script for CherryPy + Apache (or other front-end).""" import os import cherrypy class Root(object): def index(self): return "Hello World!" index.exposed = True root = Root() def start(): cherrypy.config.update({ 'log.error_file': os.path.join(os.path.dirname(__file__), 'site.log'), 'environment': 'production', }) cherrypy.tree.mount(root) cherrypy.engine.start() def serverless(): """Start with no server (for mod_python or other WSGI HTTP servers). You can also use this mode interactively: >>> import cpdeploy >>> cpdeploy.serverless() """ cherrypy.server.unsubscribe() start() def serve(): """Start with the builtin server.""" cherrypy.config.update({'log.screen': True}) start() if __name__ == "__main__": serve()

