找不到 D-Bus 方法 GetAll?

D-Bus method GetAll not found?

据我所知,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

信号'PropertiesChanged'也会在连接停用时发送。然后 "deactivated" 连接的对象路径不再存在。这就是您收到 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: