简体   繁体   中英

How do you talk to custom DBUS object with Python?

I have some custom applications that is using dbus to communicate...

import dbus
bus = dbus.SystemBus()
obj = bus.get_object(
    "org.freedesktop.DBus",
    "/org/freedesktop/DBus"
)

def listNames(names):
    for name in names:
        print "%s" % name

listNames(obj.ListNames());

I'm getting something like this coming back, example:

org.freedesktop.DBus
:1.7
test.helloworld
test.blahblah
test.customapp

At this point how do I listen or talk to those test.* applications? In fact, any application returned by the DBus's get_object .

I read https://dbus.freedesktop.org/doc/dbus-python/doc/tutorial.html but not helping for what I'm trying to do...

dbus-python is deprecated, pydbus is the modern Python binding for DBus. With pydbus:

To get a proxy object:

from pydbus import SystemBus
bus = SystemBus()
dev = bus.get('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager/Devices/0')

To see the API of a specific proxy object:

help(dev)

To call a method:

dev.Disconnect()

To read a property:

print(dev.Autoconnect)

To set a property:

dev.Autoconnect = True

To subscribe to a signal:

dev.StateChanged.connect(print)
loop.run()

More information: https://github.com/LEW21/pydbus/blob/master/doc/tutorial.rst

Disclaimer: I'm the author of pydbus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM