簡體   English   中英

通過python監視dbus消息

[英]monitoring dbus messages by python

我正在嘗試創建一個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'

在這種情況下,我可以觀看與過濾條件匹配的所有消息。 有人請幫我理解失敗的地方嗎? 謝謝

Notify是一種方法,而不是信號,因此您需要添加eavesdrop='true'作為匹配規則的一部分,以接收不適合您的消息。 如果運行dbus-monitor,您會注意到規則dbus-monitor設置中的eavesdrop密鑰。

這是行為的改變,我相信自dbus-1.5.6以來修復了bug 39450

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM