Register DBus "Hello World" service, object and method with Python

I am trying to export a service com.example.HelloWorldwith a name com.example.HelloWorld, with an object /com/example/HelloWorldand a method com.example.HelloWorld.SayHellothat 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 to make a simple DBus service using a single method that prints "hello world" (on its own standard output).

+4
source share
2 answers

dbus-python D-Bus:

import gobject
import dbus
import dbus.service

from 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()

, mainloop dbus-python :

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

Mainloop :

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

, dbus-send :

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

, , --session, --system, , , , , SayHello. , SayHello , :

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

:

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

., , DBus? Mainloops, Event Loops DBus mainloop.

+5

: " : dbus.mainloop.NativeMainLoop ".

, . ?

0

Source: https://habr.com/ru/post/1621885/


All Articles