sdbus

0.14.2 · active · verified Wed Apr 15

sdbus is a modern, asynchronous Python D-Bus library built upon `sd-bus` from `libsystemd`. It provides a robust, low-level interface for D-Bus communication, supporting both system and session buses. Currently at version 0.14.2, it maintains an active release cycle with regular updates addressing bug fixes and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create and export a simple D-Bus service using `sdbus`. It connects to the SystemBus, defines an interface with a method, exports an instance of that interface, and requests a D-Bus name, keeping the service alive until interrupted. This allows other D-Bus clients to call its methods.

import sdbus
import asyncio

class MyService(sdbus.DbusInterface):
    DBUS_NAME = "org.example.MyService"
    DBUS_PATH = "/org/example/MyObject"
    DBUS_INTERFACE = "org.example.MyInterface"

    @sdbus.dbus_method
    def SayHello(self, name: str) -> str:
        return f"Hello, {name}!"

async def main():
    # Connect to the System Bus
    bus = await sdbus.bus.SystemBus().connect()
    
    # Export an instance of MyService
    service_object = MyService()
    bus.export(service_object)
    
    # Request a D-Bus name for the service
    await bus.request_name(MyService.DBUS_NAME)
    
    print(f"Service '{MyService.DBUS_NAME}' exported on path '{MyService.DBUS_PATH}'.")
    print("To test, run in another terminal (with busctl):")
    print(f"busctl call {MyService.DBUS_NAME} {MyService.DBUS_PATH} {MyService.DBUS_INTERFACE} SayHello s 'World'")
    print("Press Ctrl+C to exit.")
    
    # Keep the service running indefinitely
    await asyncio.Event().wait()

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("\nService stopped.")

view raw JSON →