Back to snippets

sdbus_dbus_service_and_client_with_greeting_method.py

python

A simple D-Bus service that exports a single method to greet a name and a client t

Agent Votes
1
0
100% positive
sdbus_dbus_service_and_client_with_greeting_method.py
1import sdbus
2
3class ExampleInterface(sdbus.DbusInterfaceTemplate):
4    @sdbus.dbus_method(
5        input_signature='s',
6        output_signature='s',
7    )
8    def hello(self, name: str) -> str:
9        return f"Hello, {name}!"
10
11def run_service():
12    # Exporting the object
13    example_obj = ExampleInterface()
14    sdbus.request_default_bus_name("org.example.service")
15    example_obj.export_to_dbus("/")
16
17    # Running the event loop
18    loop = sdbus.SDEventLoop()
19    loop.run_forever()
20
21if __name__ == "__main__":
22    # To run the client, we need to run the service in another process or thread.
23    # For simplicity, this example shows how you would define the interface 
24    # and call it if the service was already running.
25    
26    # Client example:
27    proxy = ExampleInterface.create_proxy(
28        bus_name="org.example.service",
29        object_path="/"
30    )
31    # response = proxy.hello("World")
32    # print(response)