【问题标题】:monitoring dbus messages by python通过python监控dbus消息
【发布时间】:2012-07-17 16:37:51
【问题描述】:

我正在尝试制作一个 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'

在这种情况下,我可以查看所有符合过滤条件的消息。 有人请帮助我了解我失败的地方吗? 谢谢

【问题讨论】:

  • 这可能无法回答您的问题,但安装 qt4-dev-tools 并启动 qtdbusviewer 可能值得一试。我用它来调试我的 dbus 代码。我认为如果您在命令行上运行它,它可能会在其输出中显示 dbus 消息。干杯。
  • 看起来语法在短短 2 年内发生了很大变化。至少在我的 python 2.7.8 上,我不再使用“printf-C 样式”语法得到 any print 输出,尽管获取消息的方式似乎是正确的我。

标签: python dbus


【解决方案1】:

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

这是一种行为变化,我相信自从 dbus-1.5.6 以来,bug 39450 已修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2022-01-06
    • 2016-03-27
    • 2014-04-18
    • 2011-02-23
    • 1970-01-01
    • 2011-08-27
    相关资源
    最近更新 更多