Using PyAMF with Google App Engine

logo

This tutorial will show you how to use a PyAMF application (0.3.1 or newer) 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.

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.1.

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.x.x 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

Application

You can setup your application using the  WebAppGateway or the  WSGIGateway.

WebApp Gateway

The  WebAppGateway class allows a GAE application to handle AMF requests on the root URL and other standard HTTP requests on another URL (/helloworld in the example below).

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

import wsgiref.handlers
from google.appengine.ext import webapp
from pyamf.remoting.gateway.google import WebAppGateway

class MainPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!')

def echo(data):
    return data

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

def main():
    application_paths = [('/', WebAppGateway(services)), ('/helloworld', MainPage)]
    application = webapp.WSGIApplication(application_paths, debug=True)
    wsgiref.handlers.CGIHandler().run(application)

WSGI Gateway

If you don't want to use the pure google.appengine approach as described above, you can also use the  WSGIGateway by modifying your main.py like this:

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()

Start the server

Run this command from your application folder:

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

Test the application

Python

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!')

Flash

Create a new Adobe® Flash® 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

Attachments