Running CherryPy based apps under Google's App Engine

Google's App Engine is a new application platform from Google that allows you to run custom Python code on Google's infrastructure. From their website:
Google App Engine lets you run your web applications on Google's infrastructure. App Engine applications are easy to build, easy to maintain, and easy to scale as your traffic and data storage needs grow. With App Engine, there are no servers to maintain: You just upload your application, and it's ready to serve your users.
Several websites (including Google) make the claim that App Engine is compatible with any WSGI framework, even mentioning CherryPy by name. Unfortunately, they don't give any examples using CherryPy, and it turns out that it doesn't quite work "out of the box". Fortunately it wasn't too tricky to get it working, so I documented the steps here.

Click through to read more ...

Prerequisites


  • Install Python 2.5 if you don't already have it.
  • Install the AppEngine SDK
    You do not need a Google App Engine account to begin developing and running your own apps locally.

Create your application environment


Create a new folder where your application will live. For the example here, I'm using c:\hello

As noted above, CherryPy doesn't quite work "out of the box". I was only able to get it work with the latest beta version, but even then I had to patch it a bit. I'm sure there is a more elegant way to accomplish this than what I did (simply removing the offending code), but this works for now.

For ease of use I've provided a prepatched version of CherryPy: If you'd rather do the patching yourself, here is the patch

Add your application code


Now to get down to business:
  • Create hello.py in your application folder (i.e. c:\hello):
    hello.py
    """
    Recipe for running a CherryPy-based server under the Google AppEngine.
    There is actually nothing AppEngine-specific here - the key is setting up
    the environment correctly by following the other steps.
    """
    import cherrypy
    import wsgiref.handlers

    # application goes here ...
    class Root:
        def index(self):
            return "Hello, CherryPy! version=",cherrypy.__version__
            
        index.exposed = True

    #---------------------------------------------------------------------------
    # Start the server under Google AppEngine
    #---------------------------------------------------------------------------
    app = cherrypy.tree.mount(Root(), "/")
    wsgiref.handlers.CGIHandler().run(app)
  • Create app.yaml in the same folder:
    app.yaml
    application: hello
    version: 1
    runtime: python
    api_version: 1

    handlers:
    - url: /.*
      script: hello.py


Running your application


Assuming you placed the AppEngine installation folder in your PATH (i.e. c:\Program Files\Google\google_appengine), you can run your server like this:
cd c:\hello
dev_appserver.py .


The first time you run an App Engine application, it will prompt you about whether it should automatically check for SDK updates. I answered "yes", but I don't think that's required.

You should now be able to view your application through http://127.0.0.1:8080. When you are finished, you can stop the application with Ctrl-C in the console window.

Ctrl-C does not immediately kill the application, at least for me. I have to press Ctrl-C then go to the browser window and reload the page to get App Engine to recognize the Ctrl-C.


I hope this brief tutorial has saved you some pain. Have fun!

Update (Apr 19, 2008): A kind Googler was nice enough to set me up with an App Engine account, so I've created a tutorial site at http://boodebr.appspot.com

Written in WikklyText.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks for the tip...

I was too lazy to figure out why Ctrl-C did absolutely nothing... until I searched Googs and came upon your tip. Thanks! :)

thanks for your patch

I think CherryPy is suitable Application framework for Google App Engine.

Thanks for your reply and this patch. I invited you as developer my Google App test application.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.