找不到DBus方法GetAll?

2024-05-16 13:25:58 发布

您现在位置:Python中文网/ 问答频道 /正文

据我所知,org.freedesktop.DBus.属性.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()

错误:

^{pr2}$

Tags: theinorgimportgetifconnectionchange
1条回答
网友
1楼 · 发布于 2024-05-16 13:25:58

连接停用时也会发送“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:

相关问题 更多 >