【问题标题】:D-Bus method GetAll not found?未找到 D-Bus 方法 GetAll?
【发布时间】:2015-05-10 18:45:01
【问题描述】:

据我所知,org.freedesktop.DBus.Properties.GetAll 应该可以获取接口的属性。出于某种原因,这似乎不适用于org.freedesktop.NetworkManager.Connections.Active。有关如何使此代码工作的任何建议?


代码:

import dbus
from gi.repository import GObject
from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
loop = GObject.MainLoop()


def handle_nm_change(o):
    if 'ActiveConnections' in o:
        # This is a connection change, let's see if it's in our SSID list
        # First, get the ActiveConnection that changed:
        for c in o['ActiveConnections']:
            # Get the active connection
            dev = system_bus.get_object('org.freedesktop.NetworkManager', c)
            # Attempt to get the properties of the connection.
            devprops_iface = dbus.Interface(dev, dbus_interface='org.freedesktop.DBus.Properties')
            devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
            # if not devprops['Default']:
            #     ii = input('Device not default: ' + c)
            #     if ii == 'n':
            #         exit(0)
            appath = devprops['SpecificObject']
            if appath.startswith('/org/freedesktop/NetworkManager/AccessPoint'):
                ap = system_bus.get_object('org.freedesktop.NetworkManager', appath)
                ssid = ap.Get('org.freedesktop.NetworkManager.AccessPoint', 'Ssid',
                              dbus_interface=dbus.PROPERTIES_IFACE
                              )
                print(ssid)


if __name__ == '__main__':
    system_bus.add_signal_receiver(handle_nm_change,
                                   'PropertiesChanged',
                                   'org.freedesktop.NetworkManager'
                                   )
    loop.run()

错误:

ERROR:dbus.connection:Exception in handler for D-Bus signal:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/dbus/connection.py", line 230, in maybe_handle_message
    self._handler(*args, **kwargs)
  File "so-mockup.py", line 18, in handle_nm_change
    devprops = devprops_iface.GetAll('org.freedesktop.NetworkManager.Connection.Active')
  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 651, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Method "GetAll" with signature "s" on interface "org.freedesktop.DBus.Properties" doesn't exist

【问题讨论】:

    标签: python dbus


    【解决方案1】:

    当连接被停用时,也会发送信号“PropertiesChanged”。然后“停用”连接的对象路径不再存在。这就是您收到UnknownMethod 异常的原因。

    在获取 ActiveConnection 的属性之前,请确保它仍然存在。 尝试以下更改:

        # Get ActiveConnection upon receiving a PropertiesChanged signal
        nm = system_bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
        nm_iface = dbus.Interface(nm, dbus_interface='org.freedesktop.DBus.Properties')
        nms = nm_iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
    
        # Check current active connections
        for ac in nms:
                print("ActiveConnection:  "+ac)
    
        # This is a connection change, let's see if it's in our SSID list
        # First, get the ActiveConnection that changed:
        for c in o['ActiveConnections']:
        # Do whatever if c is in "ActiveConnections"
            if c in nms:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-26
      • 2015-12-22
      • 2016-08-21
      • 2022-12-21
      • 2018-12-09
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多