dbus-python Bindings

1.4.0 · active · verified Thu Apr 16

dbus-python provides Python bindings for libdbus, the reference implementation of the D-Bus inter-process communication (IPC) system. It enables Python applications to expose objects on the D-Bus and to invoke methods on objects exposed by other applications. The current version is 1.4.0, and releases are infrequent, typically aligned with updates to the underlying D-Bus specification or libdbus.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart connects to the system D-Bus and queries the `org.freedesktop.DBus` service to list all currently active service names. This demonstrates basic bus connection, object retrieval, and method invocation. For services that require asynchronous operations (e.g., listening for signals or exposing Python objects), integration with a main event loop (like GLib or Qt) is necessary.

import dbus

try:
    # Connect to the system bus
    # Use dbus.SessionBus() for the user's session bus
    bus = dbus.SystemBus()
    
    # Get the D-Bus 'org.freedesktop.DBus' interface on the bus
    obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
    
    # Get the 'org.freedesktop.DBus' interface itself
    iface = dbus.Interface(obj, 'org.freedesktop.DBus')
    
    # Call the ListNames method to get active service names
    names = iface.ListNames()
    
    print("Currently active D-Bus service names (system bus):")
    for name in names:
        print(f"- {name}")

except dbus.exceptions.DBusException as e:
    print(f"Error connecting to D-Bus or listing names: {e}")
    print("Ensure the D-Bus daemon is running and you have appropriate permissions.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →