CherryPy Project Download

Here is a simple application which runs as a standalone system:

import webbrowser
import cherrypy

class MyApp:
    """ Sample request handler class. """

    def index(self):
        return """<html><head><title>An example application</title></head>
<h1>This is my sample application</h1>
Put the content here...
<a href="/exit">Quit</a>
</body></html>""" = True

    def exit(self):
        raise SystemExit(0) = True

cherrypy.server.socket_port = 8765

OK, so how does it work?

First of all, you have a standard CherryPy application (class MyApp). Then you have the startup code. After you start the server, use the webbrowser module to start a browser pointing at the application's home page. Then, block. The application will now run as a normal CherryPy application.

Note the "Quit" link on the application page. This redirects the browser to the "/exit" URL, which stops the CherryPy server.

Many of the ideas here came from "Browser as Desktop UI", which describes a similar application, but using Python's SimpleHTTPServer module.


Here's a bare-bones example of how to bundle the above CherryPy application using py2exe. Assume that the above module is called, then you need a script something like this:

from distutils.core import setup
import py2exe

    # The first three parameters are not required, if at least a
    # 'version' is given, then a versioninfo resource is built from
    # them and added to the executables.
    version = "0.1.0",
    description = "Sample CherryPy standalone app with py2exe",
    name = "CherryPy sample",

    # Exclude OpenSSL, unless we are building a https server
    options = { 'py2exe': { 'excludes': 'OpenSSL', "packages": ["encodings", "email"] }},

    # targets to build. You may want to make this "windows" rather than
    # "console", to avoid a console window opening. But if you do, you
    # need some way of showing log information
    console = [""],

Older versions



cherrypy.server.socket_port = 8765


cpg.root = MyApp()"")
cpg.server.start(configMap = {'socketPort': 8765})

Assuming no timing problems (it works on my machine) the browser will start up showing the application main page. If there are timing problems, you can just wrap the webbrowser call in a timer, to delay it by a second or so. Example: threading.Timer(1,, ("",)).start()


Replace raise SystemExit(0) with:

import threading
threading.Timer(1, cherrypy.server.stop).start()
return """<html><head><title>System Terminated</title></head>
<h1 align="center">System Terminated</h1>


Replace import cherrypy with from cherrypy import cpg as cherrypy

Hosted by WebFaction

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