Getting started with Google App Engine, Python, and Flash CS4

Post to Twitter

Google App Engine is a fast and affordable way to get your applications launched in the cloud. Today we are going to use the App Engine SDK for Python to send data from Flash to App Engine and back.

To work through this you will need the following:
1. The Google App Engine SDK for Python
2. Python 2.5.4 (as of this writing Python 2.6+ is not supported in the current App Engine SDK)
3. Flash CS4 (Flash CS3 will work as well so will Flex with some adjustments)

Create a new “Flash File (ActionScript 3.0)” and save it as “HelloFlash”.

Now add a new “ActionScript File” and save it as “URLLoaderExample”.

I added a textarea (name: txtOutput), a textinput (named: txtHelloName), a label, and a submit button (named: btnSubmit).

On the first frame of the timeline I added this code:

btnSubmit.addEventListener(MouseEvent.CLICK, onClick);


function onClick(event:MouseEvent):void
{
	var urlLoader:URLLoaderExample = new URLLoaderExample(txtOutput, txtHelloName.text);
	urlLoader.run();
}

In the “URLLoaderExample.as” file I added this code:

package
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.net.*;
	import fl.controls.TextArea;
	import flash.net.URLVariables;


	public class URLLoaderExample extends Sprite
	{
		var txt:TextArea = null;
		var loader:URLLoader = null;
		var nm:String = "";

		public function URLLoaderExample(textArea:TextArea, helloName:String)
		{
			loader = new URLLoader();

			txt = textArea;
			nm = helloName;

			configureListeners(loader);
		}

		public function run()
		{
			var request:URLRequest = new URLRequest("http://127.0.0.1:8080/");

			try
			{
				var variables:URLVariables = new URLVariables();
				variables.helloname = nm;
				request.data = variables;

				request.method = URLRequestMethod.POST;
				loader.dataFormat = URLLoaderDataFormat.TEXT;

				loader.load(request);
			}
			catch (error:Error)
			{
				txt.text = txt.text + "\nUnable to load requested document.";
			}
		}

		private function configureListeners(dispatcher:IEventDispatcher):void
		{
			dispatcher.addEventListener(Event.COMPLETE, completeHandler);
			dispatcher.addEventListener(Event.OPEN, openHandler);
			dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
			dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}

		private function completeHandler(event:Event):void
		{
			var loader:URLLoader = URLLoader(event.target);
			txt.text = txt.text + "\n\n" + loader.data;
		}

		private function openHandler(event:Event):void
		{
			txt.text = txt.text + "\n\nopenHandler: " + event;
		}

		private function progressHandler(event:ProgressEvent):void
		{
			txt.text = txt.text + "\n\nprogressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal;
		}

		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			txt.text = txt.text + "\n\nsecurityErrorHandler: " + event;
		}

		private function httpStatusHandler(event:HTTPStatusEvent):void
		{
			txt.text = txt.text + "\n\nhttpStatusHandler: " + event;
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			txt.text = txt.text + "\n\nioErrorHandler: " + event;
		}
	}
}

That takes care of the Flash part so now we can move onto the Python part. Start the “Google App Engine Launcher” program (on Windows this is installed into your start menu). Create a new project and call it “helloflash”.

Open up the “main.py” file now and we will add some code to the default that is generated by the Google App Engine Launcher wizard.

The first thing is we will get rid of the “get” method and replace it with a “post” method like this:

  def post(self):
    self.response.headers['Content-Type'] = 'text/plain'
    helloname = self.request.get('helloname')
    if len(helloname) > 0:
      self.response.out.write('Hello: ' + helloname)
    else:
      self.response.out.write('Hello world!')

So the resulting main.py would look like this:

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util


class MainHandler(webapp.RequestHandler):

  def post(self):
    self.response.headers['Content-Type'] = 'text/plain'
    helloname = self.request.get('helloname')
    if len(helloname) > 0:
      self.response.out.write('Hello: ' + helloname)
    else:
      self.response.out.write('Hello world!')


def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()

You can now launch the application with the Launcher program. Go back into Flash and run the SWF, enter a name and hit the submit button. You should see something like this:

Post to Twitter

This entry was posted in ActionScript, Adobe, App Engine, Flash, Python. Bookmark the permalink.

One Response to Getting started with Google App Engine, Python, and Flash CS4

  1. deepak says:

    works like charm…

Comments are closed.