First App with Google App Engine
I thought of writing a little application using Google App Engine that performs an HTTP POST to an arbitrary URL. My thought was that since GAE runs locally, I could build a little HTTP POST utility that I would be able to use for SOAP and REST services. Instead of creating a thick client application for this purpose, I would just run it in the browser. I know, I know, this is a lame application and I could just use curl or a ton of other free utilities, but I just wanted to play around with GAE.
GAE was pretty trivial to setup and my posting class worked fine when I executed on its own. However, whenever I executed it from within the standalone GAE environment on my laptop, it would fail and would give me some exception about the socket module. After staring at my code for a bit and making sure that everything was copacetic, I thought maybe the problem lay with the httplib module. Though this seemed highly unlikely since it is in a built-in module for Python, I decided to also install httplib2 and give it a whirl. Well, needless to say that also failed with a similar exception.
At this point, I figured I should do the “smart” thing and look at the GAE documentation and lo and behold, it states:
A small percentage of native C python modules, and subsets of native C python modules are not available with Google App Engine. A full list detailing native C Python module support can be found here. The disabled modules fall in to the following categories:
* Libraries that maintain databases on disk are not enabled in Python for Google App Engine
* Sockets are disabled with Google App Engine
* The system does not allow you to invoke subprocesses, as a result some os module methods are disabled
* Threading is not available
* For security reasons, most C-based modules are disabled
* Other features that are limited:
o marshal is disabled
o cPickle is aliased to pickle
o System calls have been disabled
Well, since sockets aren’t allowed, there has to be another means by which HTTP resources can be accessed and then I came across urlfetch:
App Engine applications can communicate with other hosts over the Internet using HTTP requests. The URL fetching API can retrieve data using HTTP and HTTPS URLs.
Urlfetch does support HTTP GET, POST, HEAD, PUT, DELETE, but unfortunately it only works over ports 80 and 443. Therefore, GAE will not work as a testing utility for performing HTTP POSTs since arbitrary port numbers are a requirement.
Nevertheless, urlfetch is drop-dead simple. This is the code executing an HTTP POST:
resp = urlfetch.fetch(self.url, self.body, urlfetch.POST, self.headers)
I’ll definitely have to try playing around with GAE more and by the looks of it, it looks to be very simple to create a mashup.