5

In python, calling XML-RPC methods involves invoking methods on a proxy object:

from xmlrpclib import ServerProxy
print ServerProxy('https://example.com/rpc').api.hello_there('John')

In some other languages, like perl, you pass your method name as a method parameter.

use Frontier::Client;
$p = Frontier::Client->new(url => 'https://example.com/rpc');
$result = $p->call('api.hello_there', 'John');
print $result;

Is there a way to invoke XML-RPC methods by name, as strings, in Python?

Wedgwood
  • 993
  • 9
  • 10

1 Answers1

4

Just use getattr, as with any Python object.

func = getattr(ServerProxy('https://example.com/rpc'), "api.hello_there")
print func('John')
Glenn Maynard
  • 55,829
  • 10
  • 121
  • 131
  • You can't use getattr with dotted names; think you'd need getattr(getattr(..., 'api'), 'hello_there'). – Nicholas Riley Dec 21 '10 at 18:33
  • Maybe it works in this particular case; it doesn't work with Python objects in general (the "as with any Python object" part of your answer). – Nicholas Riley Dec 21 '10 at 19:27
  • I think that this wouldn't work in general, but xmlrpclib.ServerProxy overrides __getattr__ in a way that makes this work correctly. Thanks! – Wedgwood Dec 21 '10 at 19:50