Implementing the Google App Engine WebApp / Python Custom 404 Application Handler

I am using GAE, Google App Engine with Webapp using Python.

I am trying to implement my own error handler, or pattern, or along these lines. GAE provides some documentation here , however it does not provide enough in the example for implementation (for me).

I also covered all of these great examples in another StackOverflow question here - but can't figure out how to implement it in my current 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 () 

I tried to make another class and implement it before MainHandler , as well as a new def in MainHandler . The only time I displayed 404 is that it is displayed globally, which means that even the index.html file was "404".

The last thing I tried was to implement something like:

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

My app.yaml file :

 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 

I cannot find manuals / guides for implementing the 404 handler, just code extraction.

Thank you all very much!

Edit: As explained below, Hans, I want to return 404 when a file (or directory) is missing from the file system.

I tried:

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

to no avail. What is the correct implementation / syntax?

Edit: In Voscausa

 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 () 
+4
source share
2 answers

SOLUTION 1. Ask appengine to take care of your static files.

You may have GAE process your static files, for example:

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

This is better, since serving static files is much easier when it is done for you, and it will not take into account as much as your processor quotas.

There is only one warning. You cannot specify a processed error if the file is not found below /static . As soon as the handler picks up the URL, it will try to complete the request. If the static dir handler cannot find the URL, it will not return control to main.py , which is now your wildcard solution. It will simply return the default 404 error page from appengine.

SOLUTION 2: Submitting static files from your application

If you really have to create a custom 404 page for your statics, you have to do it in your application. (I do not advise you to do this if this is not clear). In this case, you are on the right track. You must create an additional handler, as in your example. Just rewrite one line:

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

In your application line, add your handler as follows:

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

If the file is not found, you should call self.response.set_status (404) and write any custom response message that you need.

0
source

See: http://webapp-improved.appspot.com/guide/exceptions.html for handling 404 and 500 exceptions. Webapp2 is now the preferred framework for python GAE applications.

0
source

Source: https://habr.com/ru/post/1434790/


All Articles