Wed*_*ood 5 python xml-rpc xmlrpclib call getattr
在 python 中,调用 XML-RPC 方法涉及调用代理对象上的方法:
from xmlrpclib import ServerProxy
print ServerProxy('https://example.com/rpc').api.hello_there('John')
Run Code Online (Sandbox Code Playgroud)
在其他一些语言中,例如 perl,您可以将方法名称作为方法参数传递。
use Frontier::Client;
$p = Frontier::Client->new(url => 'https://example.com/rpc');
$result = $p->call('api.hello_there', 'John');
print $result;
Run Code Online (Sandbox Code Playgroud)
有没有办法在 Python 中按名称(作为字符串)调用 XML-RPC 方法?
只需使用getattr,就像使用任何 Python 对象一样。
func = getattr(ServerProxy('https://example.com/rpc'), "api.hello_there")
print func('John')
Run Code Online (Sandbox Code Playgroud)