The task of such an information server is to respond to requests (in the case of web servers, requests from client web browsers) by returning output. Each time a request is received, the server analyzes what the request asks for, and returns the appropriate output. The two basic methods for the server to do this are the following:
* If the request identifies a file stored on disk, then return the contents of that file.
* If the request identifies an executable command and possibly arguments, then run the command and return its output.
CGI defines a standard way of doing the second. It defines how information about the server and the request is passed to the command in the form of arguments and environment variables, and how the command can pass back extra information about the output (such as the type) in the form of headers.
One good thing is that GAE provides all this functionality in-built through the 'webapp' framework. Although it is not mandatory to use this framework, it makes the task simpler. Other Python frameworks like Django, Pylons etc may also be used.
Code using webapp:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write('Hello, My first webapp application!')
application = webapp.WSGIApplication(
[('/', MainPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
It has 3 parts
1. one or more RequestHandler classes that process requests and build responses
2. a WSGIApplication instance that routes incoming requests to handlers based on the URL
3. a main routine that runs the WSGIApplication using a CGI adaptor
It functions as follows:
1. When an HTTP GET request is received by webapp, it instantiates the MainPage class and calls the 'get' method. Here, request data is available through 'self.request'. To write to the response stream, use self.response
2. The application itself is now referred to by a webapp.WSGIApplication instance.
3. run_wsgi_app() takes a WSGIApplication instance and runs it in GAE's CGI environment.
Well, thats about it. Just run the app using the techniques I had presented earlier.
For more about webapp, see this link
http://code.google.com/appengine/docs/python/tools/webapp/