webapp
application has three parts:- one or more
RequestHandler
classes that process requests and build responses - a
WSGIApplication
instance that routes incoming requests to handlers based on the URL - a main routine that runs the
WSGIApplication
using a CGI adaptor.
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 World from the webapp framework!') application = webapp.WSGIApplication( [('/', MainPage)], debug=True) def main(): run_wsgi_app(application) if __name__ == "__main__": main() |
application: webapptest version: 1 runtime: python api_version: 1 handlers: - url: / script: webapp.py |
What the above code does
- The
webapp
module is in thegoogle.appengine.ext
- This code defines one request handler,
MainPage
, mapped to the root URL (/
).We can map diffrent urls with diffrent handlers - When
webapp
receives an HTTP GET request to the URL/
, it instantiates theMainPage
class and calls the instance'sget
method. Inside the method, information about the request is available usingself.request
. Typically, the method sets properties onself.response
to prepare the response.
No comments:
Post a Comment