Search code examples
pythonfaviconweb.py

web.py development server - favicon.ico - 404 Not Found


When running a web.py application with the development server, how do you get rid of the 404 error for the favicon?

"HTTP/1.1 GET /" - 200 OK
"HTTP/1.1 GET /favicon.ico" - 404 Not Found

Everything I've been able to find about eliminating this error has to do with specifying a path to the resource in your Apache configuration. This obviously doesn't help with the development server use case. Is there a way to specify static resources in the urls tuple? Can you define a document root in the web.py application?


Solution

  • The web.py API documentation references a 'web.seeother()' function which generates a
    '303 SEE OTHER' response, redirecting a browser to a different location.
    (See http://webpy.org/docs/0.3/api#web.application)

    This is a server-side solution which doesn't require header changes in html files; particularly useful if the server isn't actually dealing with html files.

    Solution:

    Map a url route from the default /favicon.ico and create a new class to handle this route:

    # Define API Routes
    urls = (
        '/', 'index',
        '/favicon.ico', 'icon'
    )
    

    Create a (web accessible) static directory containing the favicon.ico

    Create a new class to handle this file:

    # Process favicon.ico requests
    class icon:
        def GET(self): raise web.seeother("/static/favicon.ico")
    

    Here are my server logs showing the requests:

    <ip#> - [18/Oct/2013 21:54:54] "HTTP/1.1 GET /favicon.ico" - 303 See Other
    <ip#> - [18/Oct/2013 21:54:54] "HTTP/1.1 GET /static/favicon.ico" - 200
    <ip#> - [18/Oct/2013 22:03:02] "HTTP/1.1 GET /favicon.ico" - 303 See Other
    <ip#> - [18/Oct/2013 22:03:03] "HTTP/1.1 GET /static/favicon.ico" - 304 Not Modified