我设置了一段读取 dbus 消息的代码:
def nf(bus, message):
print(message)
args = message.get_args_list()
for arg in message.get_args_list():
print("arg:" + arg)
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
bus.add_match_string_non_blocking("interface='org.freedesktop.Notifications',eavesdrop='true',member='Notify'")
bus.add_message_filter(nf)
mainloop = gobject.MainLoop()
mainloop.run()
当我启动它时,我收到启动消息 <dbus.lowlevel.SignalMessage path: /org/freedesktop/DBus, iface: org.freedesktop.DBus, member: NameAcquired, dest: :1.215>
和参数 1.215
.
但是当我用 notify-send
发送消息时我还收到消息 <dbus.lowlevel.MethodCallMessage path: /org/freedesktop/Notifications, iface: org.freedesktop.Notifications, member: Notify dest: :1.212>
但我没有得到任何争论。当我用 dbus-monitor 尝试这个时,我得到了所有参数。我错过了什么?
最佳答案
出于某种原因,arg 不喜欢与其他内容一起放入打印语句中。看来 python 不会像打印 arg[0] 这样的东西那样自动将列表项转换为字符串。
改变
print('arg:' + arg)
至
print('arg:' + str(arg))
甚至
print('%s %s' % ('arg:', arg))
关于python - 未获取 dbus 消息参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32092249/