Search code examples
pythontornadofaviconico

Python - Tornado : How to return 404 instead 500 for favicon


Everytime i run my code in Chrome. It returns two request. Second is favicon.ico

But Favicon.ico's content type is text/html and status is 500

How can i change its status to 404

i dont have favicon.ico and i dont want to

IMAGE


Solution

  • You are getting 500, probably because /favicon.ico matches different route (e.g. with template without required params).

    @Daniel B. answer is ok, (some) browsers won't request favicon - complete info can be found at How to prevent favicon.ico requests?. Additionally I would add ErrorHandler route to server 404.

    import tornado.ioloop
    import tornado.web
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("Hello, world")
    
    def make_app():
        return tornado.web.Application([
            (r"/favicon.ico", tornado.web.ErrorHandler, {'status_code': 404}),
            (r".*", MainHandler),
        ])
    
    if __name__ == "__main__":
        app = make_app()
        app.listen(8888)
        tornado.ioloop.IOLoop.current().start()
    

    There are also apple-*ico requested by Apple devices, you may want to send 404 as well.