Lately I have been reading quite a lot of articles about Google App Engine, and I just couldn’t resist trying it out myself.
Google App Engine enables you to build web applications on the same scalable systems that power Google applications.
If this is the first time you are hearing about the Google App Engine, it would be a good idea to read this introductory document.
The following post is based on the official “Getting Started” guide.
Setup is quite easy, all you need is the Google App Engine SDK, and the Python Runtime software.
I am using Windows XP, so I have downloaded the “GoogleAppEngine_1.0.1.msi” and “python-2.5.2.msi” installers from the above links.
Both of them are available for Windows/Mac/Linux, so you can download the required files according to your Operating System.
Installation is also straight-forward, I have installed both of them with the default options. There was no need to edit config files or do something else.
Google App Engine even sets a “Path” Environment Variable in Windows to make the development web server more accessible.
Coding – Part 1
Here comes the fun part, let’s find out how to build the “Hello World” application.
Create a new folder on your hard disk called “helloworld”.
Fire up your favorite text editor, mine is Notepad++, and paste the following code:
print 'Content-Type: text/plain' print '' print 'Hello World!'
Save the file as “helloworld.py” inside the folder “helloworld”.
Create a new text file called “app.yaml” in the same folder with the following contents:
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: /.* script: helloworld.py
The “app.yaml” is the configuration file, you can find more information about it here.
Now let’s start the development web server by typing in the command prompt:
Please replace the “D:\GoogleAppEngine\helloworld” with the path of your “helloworld” folder.
Point your web browser to http://localhost:8080/ , and you should see something like the following output:
Now change the “helloworld.py” a bit:
print 'Content-Type: text/plain' print '' print 'Hello World! - Google App Engine rocks!'
Save it, refresh your browser, and you should see:
The development web server is monitoring the project files, thus you can automatically view the changes!
Coding – Part 2
On this second part we are going to use the included, with the SDK, “webapp” framework.
Let’s create a new Python script file called “helloworld2.py” with the following contents:
import wsgiref.handlers from google.appengine.ext import webapp class MainPage(webapp.RequestHandler): def get(self): self.response.headers['Content-Type'] = 'text/plain' self.response.out.write('Hello World from the webapp framework!' ) def main(): application = webapp.WSGIApplication( [('/helloworld2', MainPage)], debug=True) wsgiref.handlers.CGIHandler().run(application) if __name__ == "__main__": main()
We also need to modify the configuration file (app.yaml), in order to specify the url that is going to handle the second script:
application: helloworld version: 1 runtime: python api_version: 1 handlers: - url: / script: helloworld.py - url: /helloworld2 script: helloworld2.py
As you can see, we changed the first handler (/.* -> /) to respond only to the root directory.
Also we added a second handler to respond on the /helloworld2 path.
Navigate your browser to http://localhost:8080/helloworld2 and you should see:
You can find here more information about the “webapp” framework and how to use it.
Well that was just the beginning to an exciting new way of developing web applications.
The API is under heavy development, and definitely you have to spend some time in order to get familiar with it, and build a real-world application.