[Twisted-Python] Question about multiple XML-RPC handlers
Thomas Weholt
2002 at weholt.org
Mon Aug 25 16:39:24 MDT 2003
>From the docs I see this example on how to implement a XML-RPC service :
from twisted.web import xmlrpc, server
class Example(xmlrpc.XMLRPC):
"""An example object to be published."""
def xmlrpc_echo(self, x):
"""Return all passed args."""
return x
def xmlrpc_add(self, a, b):
"""Return sum of arguments."""
return a + b
def main():
from twisted.internet.app import Application
app = Application("xmlrpc")
r = Example()
app.listenTCP(7080, server.Site(r))
return app
application = main()
if __name__ == '__main__':
application.run(save=0)
Test it with :
>>> import xmlrpclib
>>> s = xmlrpclib.Server('http://localhost:7080/')
>>> s.echo("lala")
'lala'
All is well and fine. But I need to differentiate abit, more like :
>>> import xmlrpclib
>>> s = xmlrpclib.Server('http://localhost:7080/')
>>> s.Example.echo("lala")
'lala'
For instance, if I create another XML-RPC-handler:
class Dummy(xmlrpc.XMLRPC):
def xmlrpc_subtract(self, x, y):
return x - y
I want someway to register it with the server and be able to call :
>>> import xmlrpclib
>>> s = xmlrpclib.Server('http://localhost:7080/')
>>> s.Example.echo("lala")
'lala'
>>> s.Example.add(1,2)
3
>>> s.Dummy.subtract(1,2)
-1
I've hacked the current XML-RPC code in Twisted abit to fit my needs, but
I'd like to be able to use the standard Twisted framework for this, not some
ad-hoc/mediocre/experimental thingy. Can I create a root-request which
somehow redirects the call to the correct instance/method if found or give a
proper failure-result if nothing is found?
Any clue or hint would be highly appreciated.
Best regards,
Thomas Weholt
More information about the Twisted-Python
mailing list