My favorites | 中文(简体) | Sign in

实用工具函数

google.appengine.webapp.util 包提供以下函数:

@login_required

webapp 请求处理程序方法的 Python 注释,用于验证用户是否使用 Google 帐户登录,如果未登录,会将该用户重定向至登录页面。登录页面将重定向至请求网址。

此注释仅适用于 GET 请求。

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)

在 App Engine 的 CGI 环境中运行 WSGI 应用程序。这与使用从 WSGI 到 CGI 的适配器(例如 Python 标准库的 wsgiref 模块提供的适配器)类似,然而相比之下,前者具有以下几个优势:run_wsgi_app() 可以自动检测应用程序是否在开发服务器中运行,如果在其中运行,则会将错误写入日志,而且还会输出到浏览器中。run_wsgi_app() 还允许 App Engine 在发送错误前发送处理程序输出的数据,而 wsgiref.handlers.CGIHandler 并不执行此操作。

参数:

application
WSGI 应用程序对象(例如 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()