[Twisted-web] Re: [PATCH] nevow.static.File directory listing

Matt Goodall matt at pollenation.net
Mon Apr 5 02:48:10 MDT 2004


On Mon, 2004-04-05 at 02:03, Lee Harr wrote:
> >Can you explain *why* you have the following code (copied from your
> >original post):
> >
> >
> >         from twisted.application import service
> >         from twisted.application import internet
> >
> >
> >         class ADir(rend.Page):
> >             def locateChild(self, request, segments):
> >                 path = '/'.join(segments)
> >                 return static.File(path), ()
> >
> >
> >         application = service.Application('ADirlist')
> >         webservice = internet.TCPServer(
> >             8080,
> >             appserver.NevowSite(ADir())
> >         )
> >         webservice.setServiceParent(application)
> >
> >
> >What is the use case? Why not let static.File handle everything?
> >
> 
> 
> Well... it is certainly possible that I am going about this all wrong.
> 
> I was trying to figure out how to serve images from the nevow server to
> go along with my web pages. This was the first way I figured to do that.

Aha! All becomes clear.

> If there is a better way, I am certainly willing to use it.

Yep, it's much simpler too :)

Although locateChild() is the "official" API for URL traversal,
rend.Page adds some more convenient ways of specifying children. The one
you probably want is so define a child_images attribute on your root
page's class:

        from twisted.application import internet, service
        from twisted.web import static
        
        from nevow import appserver
        from nevow import rend
        from nevow import static
        
        class RootPage(rend.Page):
        
            # Mount the 'images' directory at the '/images' URl
            child_images = static.File('images')
            
            docFactory = rend.htmlstr(
                '''
                <html>
                  <body>
                    <img src="/images/TwistedLogoFull.png" />
                    <p>Blah, blah, blah</p>
                  </body>
                </html>
                '''
                )
        
        application = service.Application('static')
        webServer = internet.TCPServer(
            8000,
            appserver.NevowSite(RootPage()))
        webServer.setServiceParent(application)

*Note* ... static.File will publish individual files or an entire
directory structure. It is up to you how you use it but for 'images',
'stylesheets', 'scripts' etc I tend to find it easier to publish a
directory for each. You could even put the 'images', 'stylesheets' and
'scripts' directories in one 'static' directory and publish the whole
thing with one child_static of something similar.

The other ways rend.Page helps you with this task are best demonstrated
by examples/children.tac in Nevow's svn repository and (I think) the
released source tar ball.

Hope this helps.

Cheers, Matt

-- 
Matt Goodall, Pollenation Internet Ltd
w: http://www.pollenation.net
e: matt at pollenation.net

Any views expressed are my own and do not necessarily reflect the
views of my employer.




More information about the Twisted-web mailing list