tools.staticdir

113 views
Skip to first unread message

Vincent Le Goff

unread,
Oct 29, 2012, 11:29:59 AM10/29/12
to cherryp...@googlegroups.com
Hi everyone,

I'm trying to get the tools.staticdir working, but it keeps on failing.

I created a directory C:\static and a file test.txt inside it. I would
like the directory content be served through the /static requests.

Here's my configuration:

conf = {
'/': {
'request.dispatch': self.dispatcher,
'tools.staticdir.root': 'C:',
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': 'static',
}
}
cherrypy.tree.mount(root=None, config=conf)

But when I send a request to:
myserver/static/test.txt

I get a 404 error.

As you can see, I'm not using the default dispatcher. Is it failing
because of that? Should I include some code in my own dispatcher
to call the hooks directly? I don't think so, but I don't know where
this problem comes from.

Thanks for your help,

Vincent

Walter Woods

unread,
Oct 29, 2012, 12:47:24 PM10/29/12
to cherryp...@googlegroups.com
Hi Vincent,

I'm not used to using a different dispatcher (the object-based one is kind of awesome), but this is the class I always use for tools.staticdir, maybe it will help:

class StaticServer(object):
    """For testing - serves static files out of a given root.
    """

    def __init__(self, rootDir):
        self.rootDir = rootDir
        if not os.path.isabs(self.rootDir):
            self.rootDir = os.path.abspath(rootDir)

    @cherrypy.expose
    def default(self, *args):
        file = os.path.join(self.rootDir, *args)
        return cherrypy.lib.static.serve_file(file)

Usage:
class Root(object):
    static = StaticServer(r'C:\static')


--
You received this message because you are subscribed to the Google Groups "cherrypy-users" group.
To post to this group, send email to cherryp...@googlegroups.com.
To unsubscribe from this group, send email to cherrypy-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cherrypy-users?hl=en.


Vincent Le Goff

unread,
Oct 29, 2012, 1:24:05 PM10/29/12
to cherryp...@googlegroups.com
Hi,

I think I could serve static content without using the tools.staticdir
(as you did with your class, I guess). I will check that out.

Thanks,

Vincent

2012/10/29, Walter Woods <woods...@gmail.com>:

Walter Woods

unread,
Oct 29, 2012, 1:34:01 PM10/29/12
to cherryp...@googlegroups.com

Yeah tools.staticdir works kinda weird... the little wrapper class makes it much nicer

Vincent Le Goff

unread,
Oct 30, 2012, 11:34:15 AM10/30/12
to cherryp...@googlegroups.com
Hi,

Thanks a lot, it's working just fine.
Reply all
Reply to author
Forward
0 new messages