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
PythonOption cherrypy.setup cpdeploy::serverless
PythonDebug On
</Location>
...assuming you've saved this template as "cpdeploy.py".
"""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 serverless(): """Start with no server (for mod_python or other WSGI HTTP servers). You can also use this mode interactively: >>> import cpdeploy >>> cpdeploy.serverless() """ # Set up site-wide config. Do this first so that, # if something goes wrong, we get a log. cherrypy.config.update({ 'log.screen': False, 'log.error_file': os.path.join(os.path.dirname(__file__), 'site.log'), 'environment': 'production', 'request.show_tracebacks': False, # Turn off signal handlers when CP does not control the OS process 'engine.SIGTERM': None, 'engine.SIGHUP': None, }) cherrypy.tree.mount(root) try: cherrypy.engine.start(blocking=False) except: cherrypy.log(traceback=True) raise def serve(): """Start with the builtin server.""" # Set up site-wide config. Do this first so that, # if something goes wrong, we get a log. cherrypy.config.update({ 'log.screen': True, 'log.error_file': os.path.join(os.path.dirname(__file__), 'site.log'), 'environment': 'production', }) cherrypy.tree.mount(root) cherrypy.server.quickstart() cherrypy.engine.start() if __name__ == "__main__": serve()

