在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的方法呢?
1 个回答
4
只需使用 getattr
,就像对待任何Python对象一样。
func = getattr(ServerProxy('https://example.com/rpc'), "api.hello_there")
print func('John')