通过python监视dbus消息

时间:2012-07-18 15:24:45

标签: python dbus

我正在尝试创建一个python应用程序来读取通过DBus传递的消息,这些消息提供了与bash dbus-monitor 相同的输出。根据我从搜索中得到的结果,代码应该非常简单明了,例如:

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop

def msg_cb(bus, msg):
    args = msg.get_args_list()
    print "Notification from '%s'" % args[0]
    print "Summary: %s" % args[3]
    print "Body: %s", args[4]

if __name__ == '__main__':
    DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()

    string = "interface='org.freedesktop.Notifications',member='Notify'"
    bus.add_match_string(string)
    bus.add_message_filter(msg_cb)

    mainloop = gobject.MainLoop ()
    mainloop.run ()

但启动它我只得到DBus返回的消息说应用程序是连接的,这与我执行bash命令时得到的不同:

dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify'

在这种情况下,我可以观看与过滤条件匹配的所有消息。 有人请帮我理解失败的地方吗? 感谢

1 个答案:

答案 0 :(得分:7)

Notify是一种方法,而不是信号,因此您需要添加eavesdrop='true'作为匹配规则的一部分,以接收不适合您的消息。如果运行dbus-monitor,您会注意到dbus-monitor规则中的eavesdrop键设置。

这是行为上的改变,我相信自dbus-1.5.6以来bug 39450被修复了。