Can XML-RPC methods be called by name (as strings) in Python?

守給你的承諾、 提交于 2019-12-10 10:32:49

问题


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?


回答1:


Just use getattr, as with any Python object.

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


来源:https://stackoverflow.com/questions/4502492/can-xml-rpc-methods-be-called-by-name-as-strings-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!