Register a Hello World DBus service, object and method using Python

2024/10/5 5:17:06

I'm trying to export a DBus service named com.example.HelloWorld, with an object /com/example/HelloWorld, and method com.example.HelloWorld.SayHello that prints "hello, world" if the method is called using

dbus-send --system --type=method_call --dest=com.example.HelloWorld /com/example/HelloWorld com.example.HelloWorld.SayHello

So my question is how do you make a simple DBus service with a single method that prints "hello, world" (on its own stdout).

Answer

When using dbus-python the following setup for exporting a D-Bus service works:

import gobject
import dbus
import dbus.servicefrom dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)OPATH = "/com/example/HelloWorld"
IFACE = "com.example.HelloWorld"
BUS_NAME = "com.example.HelloWorld"class Example(dbus.service.Object):def __init__(self):bus = dbus.SessionBus()bus.request_name(BUS_NAME)bus_name = dbus.service.BusName(BUS_NAME, bus=bus)dbus.service.Object.__init__(self, bus_name, OPATH)@dbus.service.method(dbus_interface=IFACE + ".SayHello",in_signature="", out_signature="")def SayHello(self):print "hello, world"if __name__ == "__main__":a = Example()loop = gobject.MainLoop()loop.run()

The example is modified from your code with how the mainloop is setup for dbus-python with the following lines:

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

The mainloop is started after the service has been initialized in the last section of the example:

if __name__ == "__main__":a = Example()loop = gobject.MainLoop()loop.run()

The full example above can be called by calling dbus-send like so:

dbus-send --session --print-reply --type=method_call --dest=com.example.HelloWorld /com/example/HelloWorld com.example.HelloWorld.SayHello.SayHello

Note that this line is also modified from your question by specifying --session and not --system, and that the way to specify what method to call is to append the method name to the end of the interface and thus we have the double SayHello part there. If that was not intended, you can remove the added SayHello from the interface when exporting your method in the service like this:

# only 'IFACE' is used here
@dbus.service.method(dbus_interface=IFACE,in_signature="", out_signature="")

And then the service can be called like so:

dbus-send --session --print-reply --type=method_call --dest=com.example.HelloWorld /com/example/HelloWorld com.example.HelloWorld.SayHello

Also see e.g. How to use the existing services in DBus? for more examples of minimal service and client code, and Role of Mainloops, Event Loops in DBus service for some info about the mainloop stuff.

https://en.xdnf.cn/q/70524.html

Related Q&A

Python 3 Timedelta OverflowError

I have a large database that I am loading into an in-memory cache. I have a process that does this iterating through the data day by day. Recently this process has started throwing the following error:…

Constructing hierarchy from dictionary/JSON

Im looking for a way to create hierarchy in form of child parent relationship between two or more instances of same class.How would one go about creating such objects from nested dictionary like in exa…

PyPy file append mode

I have code like this:f1 = open(file1, a) f2 = open(file1, a)f1.write(Test line 1\n) f2.write(Test line 2\n) f1.write(Test line 3\n) f2.write(Test line 4\n)When this code is run with standard Python 2.…

Clean up ugly WYSIWYG HTML code? Python or *nix utility

Im finally upgrading (rewriting ;) ) my first Django app, but I am migrating all the content. I foolishly gave users a full WYSIWYG editor for certain tasks, the HTML code produced is of course terribl…

Using config files written in Python [closed]

Closed. This question is opinion-based. It is not currently accepting answers.Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.Clo…

Model not defined when using foreign key to second model

Im trying to create several relationships between some models such as User and Country. When I try to syncdb, my console outputs "Name Country is not defined". Here is the code: class User(mo…

Sending a POST request to my RESTful API(Python-Flask), but receiving a GET request

Im trying to send a trigger to a Zapier webhook in the form of a POST request containing JSON. It works fine if I just send the POST request through a local python script.What I want to do is create a …

django deploy to Heroku : Server Error(500)

I am trying to deploy my app to heroku. Deploy was done correctly but I got Server Error(500). When I turned DEBUG true, Sever Error doesnt occurred. So I think there is something wrong with loading st…

Should I preallocate a numpy array?

I have a class and its method. The method repeats many times during execution. This method uses a numpy array as a temporary buffer. I dont need to store values inside the buffer between method calls. …

dup, dup2, tmpfile and stdout in python

This is a follow up question from here.Where I want do go I would like to be able to temporarily redirect the stdout into a temp file, while python still is able to print to stdout. This would involve …