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

查看:38
本文介绍了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: static1/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 -

推荐答案

您需要注册一个包罗万象的脚本处理程序.将此附加到您的 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天全站免登陆