a href="xn--fiqr16adi7bgnc.xn--dqr445bdw4a.xn--j6w193g">隆鎮,Hacker News,Network Security,Hacker News,1N4148
main.py - App Engine Guide,Google App Engine Guide

main.py

You can use any text editor to open the file. In this demonstration, since we have install python 2.5.4, we use the IDLE which is include in python installation as our editor.

The content of the main.py should look like the following:
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from
google.appengine.ext.webapp import util
class MainHandler(webapp.RequestHandler):
    def get(self,url):
        result = urlfetch.fetch(url="http://www.google.co.uk"
                                +url
                                +"?"
                                +self.request.query_string,
                                method=urlfetch.GET)
        content_type=result.headers.get("Content-Type")
        self.response.headers['Content-Type']=content_type
        self.response.out.write(result.content)
def main():
    application = webapp.WSGIApplication([('(.*)',MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)
if __name__ == '__main__':
    main()


The Explanation of the code:
There are three import statements:
from google.appengine.ext import webapp
from google.appengine.api import urlfetch
from google.appengine.ext.webapp import util
which means that the keyword "webapp" is referred as "google.appengine.ext.webapp", the keyword "urlfetch" is referred as "google.appengine.api.urlfetch" and the keyword "util" is referred as "google.appengine.ext.webapp.util". Please note that "words with brown colors" are always reserved words in Python programming language.

After that, there is the definition of a Python class which has the name of "MainHandler":
class MainHandler(webapp.RequestHandler):
    def get(self,url):
        result = urlfetch.fetch(url="http://www.google.co.uk"
                                +url
                                +"?"
                                +self.request.query_string,
                                method=urlfetch.GET)
        content_type=result.headers.get("Content-Type")
        self.response.headers['Content-Type']=content_type
        self.response.out.write(result.content)
We intended to create an object to handle a request for every url that under "http://localhost:8080". In order to cope with this, we have to code the handler as a Python class. Python language using indentation together with ":" to indications the ideas of "inside of" or "belongs to the scope of".
class MainHandler(webapp.RequestHandler):
For instructing the app-engine on how to response to a "get" instruction, "self" is a variable for handling response and request and "url" is parsed from main() function which will be explained later:
def get(self,url):
In our program, we want the app-engnine get the content from google uk site, we use the following statement:
        result = urlfetch.fetch(url="http://www.google.co.uk"
                                +url
                                +"?"
                                +self.request.query_string,
                                method=urlfetch.GET)
Then we want to get the correct character and content type which are important informatio for displaying, we stored them in a variable "content_type" by:
 
        content_type=result.headers.get("Content-Type")

After obtaining the content_type, we use as a header for outputing the content to client throught http protocol:
 
self.response.headers['Content-Type']=content_type
 
Finally we output the content obtained from google uk site:
self.response.out.write(result.content)

Just after the definition of the "MainHandler" class, there are the "main" function. Whenever a python file is executed (instead of run as an included file for assisting other python file), a global variable "__name__" will be assigned with a string value of '__main__' . So, it is quite often to check the content of "__name__". If the content of "__name__" is equal to '__main__', let's run the startup code stored under main(). The following is the code that serves the purpose:
if __name__ == '__main__':
    main()

Now, let's return to the startup code stored under main():
def main():
    application = webapp.WSGIApplication([
'(.*)',MainHandler)],
                                         debug=True)
    util.run wsgi_app(application)

The first line is to create a mapping between request and the corresponding handling class. In our case, we use regular expression '(.*)', which is a mapping from any location of our website to a class called MainHandler. The regular expression '(.*)' means to obtain any possible url and pass it as the second parameter in the get method of the MainHandler (url):
 
class MainHandler(webapp.RequestHandler):
    def get(self,url):
 
Then we set error message displaying mode as true (debug mode, debug=True) for displaying error message. This mapping together with the debug mode setting will be used as input parameters for WSGIApplication function to generate an application:
application = webapp.WSGIApplication(['(.*)',MainHandler)],
                                         debug=True)

This application is then launched by the command:
util.run wsgi_app(application)



Permanent Address | Contact me