Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,XML-RPC方法可以按名称(作为字符串)调用吗?_Python_Call_Xml Rpc_Getattr_Xmlrpclib - Fatal编程技术网

在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

在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方法?

只需使用
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。谢谢