dbus-next

0.2.3 · active · verified Thu Apr 16

dbus-next is a pure Python 3 library for the D-Bus message bus system, designed for interprocess communication in Linux desktop and mobile environments. It boasts zero dependencies and offers first-class asyncio support, along with an optional GLib main loop backend. It's a modern alternative to older D-Bus bindings, providing high-level client and service interfaces, and is currently at version 0.2.3 with an active development and release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates connecting to the D-Bus session bus and interacting with a media player using the MPRIS (Media Player Remote Interfacing Specification) interface. It showcases how to connect to the bus, introspect an object, obtain a proxy interface, call a method, and read a property.

import asyncio
import os
from dbus_next.aio import MessageBus

async def main():
    bus = await MessageBus().connect() # Connect to the session bus
    
    # Example: Introspect and interact with a media player (MPRIS)
    # Replace 'org.mpris.MediaPlayer2.vlc' with your player's bus name if different
    player_bus_name = os.environ.get('DBUS_PLAYER_NAME', 'org.mpris.MediaPlayer2.vlc')
    player_object_path = os.environ.get('DBUS_PLAYER_PATH', '/org/mpris/MediaPlayer2')

    try:
        introspection = await bus.introspect(player_bus_name, player_object_path)
        obj = bus.get_proxy_object(player_bus_name, player_object_path, introspection)
        player_interface = obj.get_interface('org.mpris.MediaPlayer2.Player')

        # Call a method (e.g., Play)
        print(f"Calling Play on {player_bus_name}...")
        await player_interface.call_play()
        print("Play called.")

        # Get a property (e.g., PlaybackStatus)
        status = await player_interface.get_playback_status()
        print(f"Current playback status: {status}")

    except Exception as e:
        print(f"Could not interact with media player (is one running?): {e}")
    finally:
        bus.disconnect()

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →