[Twisted-Python] twisted.web, url -> resource mapping and isLeaf questions

David david at silveregg.co.jp
Tue Nov 30 01:07:17 EST 2010


I am following "twisted in 60sec" series, but even the simple examples 
are a bit unclear to me, especially w.r.t. url mapping. For example:

# assume the right imports...
class MainPage(Resource):
     isLeaf = True
     def render_GET(self, request):
         return "somestring"

resource = MainPage()
factory = Site(resource)
reactor.listenTCP(8880, factory)

I was expecting that accessing any other 'path' besides the main url 
would cause 404, e.g. "http://localhost:8880/foo/bar", and I get the 
same page instead.

I gather this is a consequence of the isLeaf setting set to be True, 
because twisted.web stops as soon as it encounters MainPage in the 
hierarchy and use that for rendering. Setting it to False seems to avoid 
the page to be available at all. The solution I got instead is something 

class RootPage(Resource):
     isLeaf = False
     def render(self, request):
         return "somestring"

     def getChild(self, path, request):
         if path:
             return NoResource()
             return self

and adding new resources through putChild. Isn't there a more natural 
way of doing things ?



More information about the Twisted-Python mailing list