本文介绍了注册“ Hello World”使用Python的DBus服务,对象和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导出名为 com.example.HelloWorld 的DBus服务,并使用对象 / com / example / HelloWorld 和方法 com.example.HelloWorld.SayHello ,如果使用

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

所以我的问题是,如何使用一种打印 hello,world(在其自己的stdout上)的单一方法来制作简单的DBus服务。

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

推荐答案

使用 dbus-python 时,以下导出D-Bus服务的设置有效:

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

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

该示例通过以下代码从代码中进行了修改,即如何为 dbus-python 设置mainloop:

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

上面的完整示例可以通过调用<$来调用c $ c> dbus-发送像这样:

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

请注意,通过指定-session 而不是-system ,指定要调用的方法的方法是将方法名称附加到接口的末尾,因此,我们有两个 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

另请参见例如以获取有关mainloop内容的一些信息。

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.

这篇关于注册“ Hello World”使用Python的DBus服务,对象和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 11:36