2010-12-21 17 views
2

在Python中,調用XML-RPC方法涉及到一個代理對象上調用方法:在Python中,XML-RPC方法是否可以通過名稱(作爲字符串)進行調用?

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

在一些其他語言,如Perl,你通過你的方法名稱作爲方法的參數。

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

有沒有辦法在Python中按名稱調用XML-RPC方法?

回答

2

只要使用getattr,就像任何Python對象一樣。

func = getattr(ServerProxy('https://example.com/rpc'), "api.hello_there") 
print func('John') 
+0

您不能使用帶有虛線名稱的getattr;認爲你需要getattr(getattr(...,'api'),'hello_there')。 – 2010-12-21 18:33:15

相關問題