Saturday, July 24, 2010

Python and Google App Engine

Quite from some time I was getting bored of working with .Net and for some recreational work I decided to start learning Python and google app engine (On Windows) .
After installing Google AppEngine for Python and Google data service,I was ready to dive in. Google app engine works on CGI standards. I decided to implement a feedback form and send mail to users informing them about other resources. I started with creating a working directory for my application.
Google app engine applications contains a configuration file (app.yaml) that tells about the runtime to use (in my case python) and the handlers that will redirect and process the request .

application: Feedback
version: 1
runtime: python
api_version: 1

- url: /.*

In above app.yaml file url: section in handlers specify that request to the url /.* should be handled by the python script file.

Google App engine web framework defines many modules that helps us to write handlers and processors for web requests.
Google Web application typically contains multiple classes that processes the request and generates the responses. WSGIApplication class instance is responsible for routing incoming request for a particular URL to one of the handler classes.Finally there is main method to start the application.

import cgi
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
def get(self):
<form method="post" action="/submit">
Name:</br> <input type="Text" name='name' width="100" />
Address:</br> <input type="Text" name='address' width="100" />
Email: </br><input type="Text" name='email' width="100" />
<textArea name="name" rows="4" cols="60"></textarea>
<input type="submit" value="Submit"/>

class FeedBack(webapp.RequestHandler):
def post(self):
self.response.out.write('<html><body>Thanks for your feed back.')
<p>Email has been sent to you containing
futher resources.Thanks.
mail.send_mail(sender=" Support <>",
subject="Your account has been approved",
Hello : ' + cgi.escape(self.request.get('name'))
+ ' </p>You can access the following resources .
<a href="" >Res1</a>

application = webapp.WSGIApplication(
[('/', MainPage),
('/submit', FeedBack)],

def main():

if __name__ == "__main__":

Above feedback ( implementation contains two handlers classes (MainPage and FeedBack) inheriting the webapp.Requesthandler class. These classes implement the post and get method that handles the incoming requests. Self.request and Self.response are the objects that are used to get the request parameters and generate the response. Apart from the handlers classes, WSGIApplication instance is created that specifies the URL's mapped to the handler classes, debug=True specify to print the error info on page if any. Then there is main method that runs the application.

As configured in the app.yaml , the request at base Url initiate the, which then routes the request to the handler class , based on WSGIApplication mapping. Main handler displays the feedback form with post method and action equal to "/submit" url. On submitting the MainPage form , url is redirected to "/submit" url , which as defined in the WSGIApplication, routes the request to feedBack handler class. Feedback handler class creates the response and sends a mail to user.

To run the application we can use command line or Google App engine launcher.
Start the Google App engine Launcher and use add application option to add the working directory.Google App engine launcher provides a simple UI to run the application.

Google data services contains various modules that can be used to access and use various google services such as youtube , picasa , facebook etc. Routing mechanism is somewhat same as MVC. What I found is that Google app engine development is tightly integrated with other google services and exposes rich api to interact with other google services.

Google App Engine


No comments:

Post a Comment