在Python中,XML-RPC方法可以按名称(作为字符串)调用吗?
在python中,调用XML-RPC方法涉及调用代理对象上的方法:在Python中,XML-RPC方法可以按名称(作为字符串)调用吗?,python,call,xml-rpc,getattr,xmlrpclib,Python,Call,Xml Rpc,Getattr,Xmlrpclib,在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.co
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方法?只需使用
getattr
,就像使用任何Python对象一样
func = getattr(ServerProxy('https://example.com/rpc'), "api.hello_there")
print func('John')
不能将getattr与虚线名称一起使用;认为您需要getattr(getattr(…,'api'),'hello_there')。也许它在这种特殊情况下有效;一般来说,它不适用于Python对象(答案中的“与任何Python对象一样”部分)。我认为这在一般情况下都不适用,但xmlrpclib.ServerProxy会以一种正确的方式覆盖getattr。谢谢