可以在Python中按名称(作为字符串)调用XML-RPC方法吗? [英] Can XML-RPC methods be called by name (as strings) in Python?

查看:120
本文介绍了可以在Python中按名称(作为字符串)调用XML-RPC方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,调用XML-RPC方法涉及在代理对象上调用方法:

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')

在其他语言(例如perl)中,您将方法名称作为方法参数传递.

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;

有没有一种方法可以在Python中通过名称(如字符串)来调用XML-RPC方法?

Is there a way to invoke XML-RPC methods by name, as strings, in Python?

推荐答案

与任何Python对象一样,只需使用getattr.

Just use getattr, as with any Python object.

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

这篇关于可以在Python中按名称(作为字符串)调用XML-RPC方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆