【问题标题】:Get 'state' from dbus using python使用 python 从 dbus 获取“状态”
【发布时间】:2019-07-20 04:00:37
【问题描述】:

我想在 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 中正确使用它。

这是对我的问题最接近的 SO 答案,但我仍然无法弄清楚。 [dbus_to_python() takes exactly 1 argument?

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

【问题讨论】:

  • 解决方案如下:

标签: python dbus firewalld


【解决方案1】:

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

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 服务或对象。

【讨论】:

  • 谢谢。我最终弄清楚了这一点,并在您发布回复的同时发布了答案。
【解决方案2】:

解决办法如下:

Proxy.Get('org.fedoraproject.FirewallD1', 'state', dbus_interface='org.freedesktop.DBus.Properties')

【讨论】:

    猜你喜欢
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多