5

I'm starting to learn cherrypy but I've run in to a roadblock. I can't get static files to save my life. I'm getting a 404. The path '/static' was not found. I've googled however have yet to find a solution. All I want to do is serve files at http://localhost:8080/static

Suggetions?

import os
import cherrypy

class Root(object):
    @cherrypy.expose
    def index(self):
        pass

config = {
    '/static':{
    'tools.staticdir.on': True,
    'tools.staticdir.dir': os.path.join(os.path.dirname(__file__), 'static')
    }
}

cherrypy.tree.mount(Root(), '/', config = config)
cherrypy.engine.start()
Kylee
  • 1,625
  • 1
  • 31
  • 54

2 Answers2

7

Some ideas:

  1. In CherryPy 3.2+, try tools.staticdir.debug = True, combined with log.screen = True or some other more preferred logging setup. That will help more than anything I can guess at in this answer.
  2. Try tools.staticdir.dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static')); it needs to be absolute (or, if .dir is not absolute, then tools.staticdir.root needs to be).
  3. In CherryPy 3.1 and above, you usually need to call engine.block() after engine.start().
fumanchu
  • 14,419
  • 6
  • 31
  • 36
  • I added the debug and log lines and the path its checking is absolute. Here is the message: `Checking File: e:\\python\\cherrypyapp\\static\\` I double checked that path and its correct but I'm still getting a 404 – Kylee Mar 25 '11 at 12:43
  • 1
    Are you actually browsing to `http://localhost:8080/static`, as if you're trying to get a directory listing in your browser? The staticdir Tool doesn't provide index pages for the files it serves--you have to request an individual file, not a directory. – fumanchu Mar 26 '11 at 01:44
  • It was the engine.block() that did it. Thanks! – Kylee Mar 26 '11 at 12:21
0

Try this

web_config = {'/': {
    'tools.staticdir.on': True,
    'tools.staticdir.root' : os.path.abspath(os.path.join(os.path.dirname(__file__))),
    'tools.staticdir.dir' : os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
},

    "/favicon.ico": {
        'tools.staticfile.on': True,
        'tools.staticfile.filename': "favicon.ico",
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "images",
    },
    '/robots.txt': {
        'tools.staticfile.on': True,
        'tools.staticfile.filename': "robots.txt",
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "", },

    '/images': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "images",
    },

    '/css': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "css",
    },

    '/js': {
        'tools.staticdir.on': True,
        'tools.staticdir.dir': "js",
    }
}

And started with

if __name__ == "__main__":

    cherrypy.config.update(server_config)
    cherrypy.tree.mount(WebSpid(), config=web_config)

    if hasattr(cherrypy.engine, 'block'):
        # 3.1 syntax
        cherrypy.engine.start()
        cherrypy.engine.block() 
Tim Seed
  • 5,119
  • 2
  • 30
  • 26