【问题标题】:Google App Engine WebApp/Python Custom 404 Handler ImplementationGoogle App Engine WebApp/Python 自定义 404 处理程序实现
【发布时间】:2012-09-10 12:03:05
【问题描述】:

我正在使用 GAE,Google App Engine 和使用 Python 的 Webapp。

我一直在尝试实现自定义错误处理程序或模板,或者按照这些思路。 GAE 提供了一些文档here,但是它在示例中没有提供足够的实现(对我来说)。

我还在另一个 StackOverflow 问题 here 上查看了所有这些精彩示例 - 但无法理解如何将其实现到我当前的 main.py 文件中。

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
 def get (self, q):
    if q is None:
      q = 'static/index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=False)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

我尝试创建另一个类,并在MainHandler 之前实现它,并在MainHandler 中实现一个新的def。我唯一一次显示 404,它是全局显示的,这意味着即使 index.html 文件也是“404”。

我尝试的最后一件事是按照以下方式实现:

if not os.path.exists (_file_):
 self.redirect(/static/error/404.html)

我的 app.yaml 文件是:

application: appname
version: 1
runtime: python
api_version: 1

error_handlers:
  - file: static/error/404.html

  - error_code: over_quota
    file: static/error/404.html

handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css))
  static_files: \1
  upload: (.*\.(gif|png|jpg|ico|js|css))

- url: /robots.txt
  static_files: robots.txt
  upload: robots.txt 

- url: /favicon.ico
  static_files: favicon.ico
  upload: favicon.ico    

- url: .*
  script: main.py

我找不到任何有关 404 处理程序实现的指南/教程,只有代码摘录。

非常感谢大家!

编辑: 根据下面 Hans 的说明,当文件系统中不存在文件(或目录)时,我想返回 404。

我试过了:

class ErrorHandler(webapp.RequestHandler):
  def get(self):
     if not os.path.isfile(_file_):
        self.redirect('/static/error/404.html')

无济于事。正确的实现/语法是什么?

编辑: 前往沃斯考萨

import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
import logging

import webapp2

def handle_404(request, response, exception):
    logging.exception(exception)
    response.write('Oops! I could swear this page was here!')
    response.set_status(404)

def handle_500(request, response, exception):
    logging.exception(exception)
    response.write('A server error occurred!')
    response.set_status(500)

app = webapp2.WSGIApplication([
    webapp2.Route('/', handler='handlers.HomeHandler', name='home')
])
app.error_handlers[404] = handle_404
app.error_handlers[500] = handle_500

class MainHandler(webapp.RequestHandler):
  def get (self, q):
    if q is None:
      q = 'static/index.html'

    path = os.path.join (os.path.dirname (__file__), q)
    self.response.headers ['Content-Type'] = 'text/html'
    self.response.out.write (template.render (path, {}))

def main ():
  application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
  util.run_wsgi_app (application)

if __name__ == '__main__':
  main ()

【问题讨论】:

    标签: python google-app-engine web-applications


    【解决方案1】:

    解决方案 1:让 appengine 处理您的静态文件

    您可以让 GAE 为您处理静态文件,如下所示:

    application: appname
    version: 1
    runtime: python
    api_version: 1
    
    handlers:
    - url: /static
      static_dir: static
    
    - url: .*
      script: main.py
    

    这更好,因为为您提供静态文件会更容易,而且它也不会计入您的 CPU 配额。

    只有一个警告。如果在/static 下找不到文件,则无法指定处理的自定义错误。一旦处理程序接收到一个 url,它就会尝试完成请求。如果静态 dir 处理程序找不到 url,它不会将控制权交还给main.py,现在这是您的通配符解决方案。它只会从 appengine 返回默认的 404 错误页面。

    解决方案 2:从您的应用程序中提供静态文件

    如果您真的必须为您的静态创建一个自定义 404 页面,您必须在您的应用程序中执行此操作。 (我不建议你这样做,以防不清楚。)在那种情况下,你是在正确的轨道上。您应该像示例中那样创建一个额外的处理程序。改写一行:

    path = os.path.join(os.path.dirname(__file__), 'static', q)
    

    在您的应用程序行中,像这样添加您的处理程序:

    ('/static/(.*)?', MainHandler),
    

    如果找不到该文件,您应该调用 self.response.set_status(404) 并编写您需要的任何自定义响应消息。

    【讨论】:

    • 不太清楚该怎么做,我已经尝试过前者,如果我的 GAE 项目中不存在正在访问的文件或目录,我希望显示 404。我以前和现在都尝试过带有 os.path.exists 和 os.pathisfile 的 ErrorHandler,请检查原始问题以对您进行编辑。
    • 澄清一下,你为什么不让GAE系统为你处理静态文件?那是因为你想指定一个自定义错误页面吗?
    • 另外,我发现在您的处理程序中,您从与您的应用程序所在的同一目录中查找文件。通常最好将文件放在不同的子目录中。
    • GAE 处理静态文件是否更好?我将所有静态 .html 文件上传到文件夹 /static/ 以及我认为适合管理页面内容的子目录。我什至不太确定 GAE 对静态内容的默认处理,我所知道的只是以我目前“有效”的特定方式进行设置。
    • 是否可以在文件名之前附加当前目录名并进行存在/isfile 检查?为什么需要将 .py/.yaml 文件更改为另一个子目录?
    【解决方案2】:

    看看:http://webapp-improved.appspot.com/guide/exceptions.html 处理 404 和 500 异常。 Webapp2 现在是 GAE python 应用程序的首选框架。

    【讨论】:

    • 我不太明白:“(请求,响应,异常)”需要填写,对吗?这是一个不完整的例子。此外,“response.set_status(404)”你是在设置错误而不是捕获错误并根据它显示错误页面?
    • NO,错误首先被处理程序捕获,显示自定义消息并设置响应头。所以客户端会收到一个带有自定义消息的 404(响应头)。
    • 复制使用,还是有问题。我的所有页面都是全局 404 的,即使它们已上传并从正确的 URL 访问。您能否在回答中提供一个适当的示例,说明如何在我的问题中显示的 main.py 文件中实现它以从中学习?谢谢。
    • 你的 app.yaml 中有什么?这些行应该是您的应用程序 yaml 中的最后一行: - url: /.* script: main.py
    • 根据我已经概述的原始问题代码,这些确实是我文件中的最后几行。
    猜你喜欢
    • 2012-12-29
    • 2012-07-31
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 2017-02-02
    • 1970-01-01
    相关资源
    最近更新 更多