0

我想在 python 中做一件非常简单的事情——通过 dbus 从 firewalld 读取状态——“org.freedesktop.DBus.Properties”。

根据https://firewalld.org/documentation/man-pages/firewalld.dbus.html

state - s - (ro)
firewalld state. This can be either INIT or RUNNING. In INIT state, firewalld is starting up and initializing.

我找到了如何使用来自 linux 的 dbus-send 来做到这一点,但我无法在 python 中正确使用它。

这是对我的问题最接近的答案,但我仍然无法弄清楚。[ dbus_to_python() 正好需要 1 个参数?

Here's how to do it in shell:
firewall-cmd --state
running 

And with dbus-send:
dbus-send --system --print-reply --dest=org.fedoraproject.FirewallD1 /org/fedoraproject/FirewallD1 org.freedesktop.DBus.Properties.Get string:"org.fedoraproject.FirewallD1" string:"state"
method return time=1563593597.133270 sender=:1.2 -> destination=:1.116 serial=691 reply_serial=2
   variant       string "RUNNING"
# Here's my incorrect code:
import dbus
Bus = dbus.SystemBus()
Proxy = Bus.get_object('org.fedoraproject.FirewallD1', '/org/fedoraproject/FirewallD1')
Proxy.Get('state', dbus_interface='org.freedesktop.DBus.Properties')

这是我从 python 得到的错误信息:

ERROR:dbus.connection:Unable to set arguments ('state',) according to signature 'ss': <class 'TypeError'>: More items found in D-Bus signature than in Python arguments
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/usr/lib/python3/dist-packages/dbus/connection.py", line 641, in call_blocking
    message.append(signature=signature, *args)
TypeError: More items found in D-Bus signature than in Python arguments
>>> 

4

2 回答 2

0

这是解决方案:

Proxy.Get('org.fedoraproject.FirewallD1', 'state', dbus_interface='org.freedesktop.DBus.Properties')
于 2019-07-20T05:29:35.017 回答
0

问题是接口的Get方法org.freedesktop.DBus.Properties实际上需要两个参数来访问接口的属性和该属性的名称:

org.freedesktop.DBus.Properties.Get (in STRING interface_name,
                                     in STRING property_name,
                                     out VARIANT value);

来自https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties

import dbus
Bus = dbus.SystemBus()
Proxy = Bus.get_object('org.fedoraproject.FirewallD1', '/org/fedoraproject/FirewallD1')
Proxy.Get('org.fedoraproject.FirewallD1', 'state', dbus_interface='org.freedesktop.DBus.Properties')

使用不同的常量进行测试,因为我没有特定的 DBus 服务或对象。

于 2019-07-20T06:07:01.163 回答