Using PyAMF with Google App Engine

http://code.google.com/appengine/images/appengine_lowres.jpg

This tutorial will show you how to use a PyAMF (>= 0.3.1) application with Google App Engine.

Introduction

Google App Engine (GAE) lets you run your web applications on Google's infrastructure for free. You can serve your app using a free domain name on the appspot.com domain, or use Google Apps to serve it from your own domain. appspot.com gives you up to 500MB of persistent storage and enough CPU and bandwidth for about 5 million page views a month.

GAE applications are implemented using Python 2.5.2. The runtime environment includes the full Python language and most of the Python standard library, including Django 0.96.

Prerequisites

Before you can start using GAE you need to:

Example Project

Setup

Start a new GAE project:

  • Create a new folder for your project.
  • Copy main.py, app.yaml, and index.yaml from google_appengine/new_project_template to your new folder. On a Mac you can find it under /usr/local.
  • Move the pyamf folder from your unpacked PyAMF-0.3.1 folder to the root folder of the new GAE project.

Your folder structure of your project should now look something like this:

 + MyProject
   - main.py
   - app.yaml
   - index.yaml
   - pyamf

The main.py module tells the GAE what code to launch. Modify it to run PyAMF:

import wsgiref.handlers
from pyamf.remoting.gateway.wsgi import WSGIGateway

def echo(data):
    return data

services = {
    'myservice.echo': echo,
}

def main():
    application = WSGIGateway(services)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
    main()

And start the server:

dev_appserver.py --debug --address=localhost --port=8080 .

Test with Python client

To test the gateway you can use a Python AMF client like this:

from pyamf.remoting.client import RemotingService

gw = RemotingService('http://localhost:8080/')
service = gw.getService('myservice')

print service.echo('Hello World!')

Test with Flash client

Create a new Flash CS3 document and place a TextField on the stage. Make it dynamic in the Properties pane, and give it the instance name output.
Then, paste the following code into the Actions pane:

import flash.net.*;

var netConnection:NetConnection = new NetConnection();
netConnection.connect("http://localhost:8080/");

var responder:Responder = new Responder(onComplete, onFail);
netConnection.call("myservice.echo", responder, "Flash talked to PyAMF.  They both say hello.");

function onComplete(results) {
        output.htmlText = results;
}

function onFail(results) {
        for each (var thisResult in results){
                output.text += thisResult;
        }
}

Run Debug > Debug movie to test PyAMF with Google App Engine! Other examples for Flex etc can be found on the Examples page.

Future Plans

This method works fine as long as the app is only going to be the gateway. It would be beneficial, however, to have the SWF running on the same instance.

If you have suggestions join us on the IrcChannel or use the MailingList.

See Also