[Twisted-Python] map Url to function

Mario Ruggier mario at ruggier.org
Mon Jun 30 10:21:47 MDT 2003


You may use getChild() on each child to traverse down the URL path
(and doing whatever you want at each step) or, if as you say you want
to make mainhandler just treat the rest of the url path as parameter
data, then you can declare your resource as a leaf, as below (this
just prints the dict of the request -- in particular look at the values
for path, prepath & postpath). But see also the web howto's.

## reqdict.rpy (mainhandler)

from twisted.web import resource

def prettyDict(name,d):
     s = []
     s.append('<table width="100%" border="0">')
     s.append('<tr><th align="left" colspan="2">'+name+'</th></tr>')
     for key in d.keys():
         s.append('<tr>')
         s.append('<td width="25%">' +key+ 
'</td><td>'+str(d[key])+'</td>')
         s.append('</tr>')
     s.append('<tr>')
     s.append('</tr>')
     s.append('</table>')
     return '\n'.join(s)

class TestResource(resource.Resource):
     isLeaf = 1
     def render(self, request):
         return prettyDict('request', request.__dict__ )

resource = TestResource()
##

Cheers, mario


On lundi, juin 30, 2003, at 17:45 Europe/Amsterdam, Achim Domma 
((ProCoders)) wrote:

> Hi,
>
> I started playing around with twisted.web and want to map the Url to
> function calls with parameters. For example
>
> http://myserver.net/mainhandler/somefkt/parama/paramb
>
> should be call some 'mainhandler'. This should be a script which gets
> '/somefkt/parama/paramb' and knows what to do with it. What's the 
> easiest
> way to do this?
>
> regards,
> Achim
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python





More information about the Twisted-Python mailing list