Google App Engine和404错误 [英] Google App Engine and 404 error

查看:104
本文介绍了Google App Engine和404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用其他地方的提示在GAE上设置了一个静态网站,但无法弄清楚如何返回404错误。我的app.yaml文件看起来像

I've setup a static website on GAE using hints found elsewhere, but can't figure out how to return a 404 error. My app.yaml file looks like

- url: (.*)/
  static_files: static\1/index.html
  upload: static/index.html

- url: /
  static_dir: static

,所有静态html / jpg文件存储在静态目录下。上述工作适用于存在的文件,但如果不存在则返回空长文件。答案可能是编写一个python脚本来返回一个404错误,但是你如何设置一些东西来为存在的静态文件提供服务,但是为那些没有的文件运行脚本?

with all the static html/jpg files stored under the static directory. The above works for files that exist, but returns a null length file if they don't. The answer is probably to write a python script to return a 404 error, but how do you set things up to serve the static files that exist but run the script for files that don't?

以下是从开发应用程序服务器上获取不存在的文件(nosuch.html)的日志:

Here is the log from fetching a non-existent file (nosuch.html) on the development application server:

ERROR    2008-11-25 20:08:34,084 dev_appserver.py] Error encountered reading file "/usr/home/ctuffli/www/tufflinet/static/nosuch.html":
[Errno 2] No such file or directory: '/usr/home/ctuffli/www/tufflinet/static/nosuch.html'
INFO     2008-11-25 20:08:34,088 dev_appserver.py] "GET /nosuch.html HTTP/1.1" 404 -


推荐答案

您需要注册一个catch-all脚本处理程序。在你的app.yaml的末尾追加这个:

You need to register a catch-all script handler. Append this at the end of your app.yaml:

- url: /.*
  script: main.py

在main.py中,您需要输入以下代码:

In main.py you will need to put this code:

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

class NotFoundPageHandler(webapp.RequestHandler):
    def get(self):
        self.error(404)
        self.response.out.write('<Your 404 error html page>')

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

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()

<您的404错误html页面> 替换为有意义的内容。或者更好地使用模板,您可以阅读此处

Replace <Your 404 error html page> with something meaningful. Or better use a template, you can read how to do that here.

如果您在设置时遇到问题,请告诉我。

Please let me know if you have problems setting this up.

这篇关于Google App Engine和404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆