首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >D-Bus python服务示例

D-Bus python服务示例
EN

Stack Overflow用户
提问于 2020-06-04 01:01:27
回答 1查看 268关注 0票数 0

我在运行最简单的D-Bus服务时遇到了麻烦。下面是我尝试使用的代码

代码语言:javascript
复制
#!/usr/bin/python3
from gi.repository import GLib
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

class Example(dbus.service.Object):
    def __init__(self, object_path):
        dbus.service.Object.__init__(self, dbus.SessionBus(), object_path)
        self._last_input = None

    @dbus.service.method(dbus_interface='com.example.Sample',
                         in_signature='v', out_signature='s')
    def StringifyVariant(self, var):
        self.LastInputChanged(var)      # emits the signal
        return str(var)

    @dbus.service.signal(dbus_interface='com.example.Sample',
                         signature='v')
    def LastInputChanged(self, var):
        # run just before the signal is actually emitted
        # just put "pass" if nothing should happen
        self._last_input = var

    @dbus.service.method(dbus_interface='com.example.Sample',
                         in_signature='', out_signature='v')
    def GetLastInput(self):
        return self._last_input

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

    session_bus = dbus.SessionBus()
    name = dbus.service.BusName("com.example.SampleService", session_bus)
    object = Example('/com/example/Sample')

    mainloop = GLib.MainLoop()
    print ("Running sample service.")
    mainloop.run()

然后我添加了/usr/share/dbus-1/services/com.example.Sample.service

代码语言:javascript
复制
[D-BUS Service]
Name=com.example.Sample
Exec=/home/me/dbus_test/service.py

权限是否正确:

代码语言:javascript
复制
~/dbus_test $ ll
-rwxrwxr-x  1 me me 1345 Jun  3 19:52 service.py*

但我无法连接到该服务

代码语言:javascript
复制
$ dbus-send --session  --dest="com.example.Sample" --type="method_call" --print-reply "/com/example/Sample" "com.example.Sample.GetLastInput"

Error org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.

我只是暂停一下。

我遗漏了什么?

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-04 03:51:53

您的服务名称是com.example.SampleService,但是您的测试客户端使用com.example.Sample作为目的地(并且服务文件也有相同的错误)。

我推荐使用D- spot来“调试”D-Bus:这样的错误在那里更容易发现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62178485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档