据我所知,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()
连接停用时也会发送“PropertiesChanged”信号。则“停用”连接的对象路径不再存在。这就是您收到
UnknownMethod
异常的原因。在在获取ActiveConnection的属性之前,请确保它仍然存在。 尝试以下更改:
相关问题 更多 >
编程相关推荐