English | Site Directory

Utility Functions

The google.appengine.webapp.util package provides the following functions:

@login_required

A Python annotation for webapp request handler methods that verifies that the user is signed in with a Google account, and redirects the user to the sign-in page if not signed in. The sign-in page redirects back to the request URL.

This annotation only works for GET requests.

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import login_required

class MyHandler(webapp.RequestHandler):

  @login_required
  def get(self):
    user = users.GetCurrentUser(self)
    self.response.out.write('Hello, ' + user.nickname())
    
run_wsgi_app(application)

Runs a WSGI application in App Engine's CGI environment. This is comparable to using a WSGI-to-CGI adaptor such as the one provided by the wsgiref module of the Python standard library, but has several advantages: run_wsgi_app() automatically detects whether the application is running in the development server, and if so outputs errors to the browser in addition to the log. run_wsgi_app() also allows App Engine to send data output by the handler prior to the error, which wsgiref.handlers.CGIHandler doesn't do.

Arguments:

application
A WSGI application object, such as webapp.WSGIApplication.
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, webapp World!')

application = webapp.WSGIApplication([('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()