学习D-Bus的python调用

12 篇文章 0 订阅

需要学习D-Bus,在网上找了一些资料和示例代码进行了调试,记录如下

D-Bus的介绍

先推荐几篇文章

示例代码

  • 测试环境是Ubuntu18.04
  • 代码来源是这里
  • 针对python3进行了一些修改
    • 修改print的调用格式
    • 修改引用
原来的import gobject已经不能用了
需要先安装
 sudo apt install python3-gi -y
然后替换引用为
from gi.repository import GObject as gobject
  • 另外加了一个通过D-Bus执行Linux的命令并返回值的步骤
  • service代码如下
dbus@Ubuntu18:~/dbus$ pip3 freeze | grep dbus
dbus-python==1.2.18
dbus@Ubuntu18:~/dbus$ 
dbus@Ubuntu18:~/dbus$ cat example-service.py 
#!/usr/bin/env python

usage = """Usage:
python example-service.py &
python example-client.py
python example-async-client.py
python example-client.py --exit-service
"""

# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

#import gobject
from gi.repository import GObject as gobject

import dbus
import dbus.service
import dbus.mainloop.glib
import os

class DemoException(dbus.DBusException):
    _dbus_error_name = 'com.example.DemoException'

class SomeObject(dbus.service.Object):

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='s', out_signature='as')
    def HelloWorld(self, hello_message):
        print(str(hello_message))
        return ["Hello", " from example-service.py", "with unique name",
                session_bus.get_unique_name()]

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='', out_signature='')
    def RaiseException(self):
        raise DemoException('The RaiseException method does what you might '
                            'expect')

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='', out_signature='(ss)')
    def GetTuple(self):
        return ("Hello Tuple", " from example-service.py")

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='', out_signature='a{ss}')
    def GetDict(self):
        return {"first": "Hello Dict", "second": " from example-service.py"}
    
    @dbus.service.method("com.example.SampleInterface",
                         in_signature='', out_signature='a{ss}')
    def GetEdition(self):
        p = os.popen('cat /etc/issue')
        result = p.read()
        return {'Linux Edition': result}

    @dbus.service.method("com.example.SampleInterface",
                         in_signature='', out_signature='')
    def Exit(self):
        mainloop.quit()


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 = SomeObject(session_bus, '/SomeObject')

    mainloop = gobject.MainLoop()
    print("Running example service.")
    print(usage)
    mainloop.run()
dbus@Ubuntu18:~/dbus$ 
  • client代码如下
dbus@Ubuntu18:~/dbus$ cat example-client.py 
#!/usr/bin/env python

usage = """Usage:
python example-service.py &
python example-client.py
python example-client.py --exit-service
"""

# Copyright (C) 2004-2006 Red Hat Inc. <http://www.redhat.com/>
# Copyright (C) 2005-2007 Collabora Ltd. <http://www.collabora.co.uk/>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import sys
from traceback import print_exc

import dbus

def main():
    bus = dbus.SessionBus()

    try:
        remote_object = bus.get_object("com.example.SampleService",
                                       "/SomeObject")

        # you can either specify the dbus_interface in each call...
        hello_reply_list = remote_object.HelloWorld("Hello from example-client.py!",
            dbus_interface = "com.example.SampleInterface")
    except dbus.DBusException:
        print_exc()
        print(usage)
        sys.exit(1)

    print(hello_reply_list)
    print('\n')

    # ... or create an Interface wrapper for the remote object
    iface = dbus.Interface(remote_object, "com.example.SampleInterface")

    hello_reply_tuple = iface.GetTuple()

    print(hello_reply_tuple)
    print('\n')

    hello_reply_dict = iface.GetDict()

    print(hello_reply_dict)
    print('\n')
    # D-Bus exceptions are mapped to Python exceptions
    try:
        iface.RaiseException()
    except dbus.DBusException as e:
        print(str(e))
        print('\n')
    
    # introspection is automatically supported
    print(remote_object.Introspect(dbus_interface="org.freedesktop.DBus.Introspectable"))
    
    # Get Linux Edition
    linux_edition = iface.GetEdition()
    print(linux_edition)
    print('\n')
    

    if sys.argv[1:] == ['--exit-service']:
        iface.Exit()

if __name__ == '__main__':
    main()
dbus@Ubuntu18:~/dbus$ 

运行

  • 在终端1运行service,不返回
dbus@Ubuntu18:~/dbus$ python3 example-service.py 
Running example service.
Usage:
python example-service.py &
python example-client.py
python example-async-client.py
python example-client.py --exit-service


  • 在终端2运行client
dbus@Ubuntu18:~/dbus$ python3 example-client.py 
dbus.Array([dbus.String('Hello'), dbus.String(' from example-service.py'), dbus.String('with unique name'), dbus.String(':1.90')], signature=dbus.Signature('s'))


dbus.Struct((dbus.String('Hello Tuple'), dbus.String(' from example-service.py')), signature=None)


dbus.Dictionary({dbus.String('first'): dbus.String('Hello Dict'), dbus.String('second'): dbus.String(' from example-service.py')}, signature=dbus.Signature('ss'))


com.example.DemoException: The RaiseException method does what you might expect


<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node name="/SomeObject">
  <interface name="org.freedesktop.DBus.Introspectable">
    <method name="Introspect">
      <arg direction="out" type="s" />
    </method>
  </interface>
  <interface name="com.example.SampleInterface">
    <method name="HelloWorld">
      <arg direction="in"  type="s" name="hello_message" />
      <arg direction="out" type="as" />
    </method>
    <method name="RaiseException">
    </method>
    <method name="GetTuple">
      <arg direction="out" type="(ss)" />
    </method>
    <method name="GetDict">
      <arg direction="out" type="a{ss}" />
    </method>
    <method name="GetEdition">
      <arg direction="out" type="a{ss}" />
    </method>
    <method name="Exit">
    </method>
  </interface>
</node>

dbus.Dictionary({dbus.String('Linux Edition'): dbus.String('Ubuntu 18.04.3 LTS \\n \\l\n\n')}, signature=dbus.Signature('ss'))


dbus@Ubuntu18:~/dbus$ 

如果遇到

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.NotSupported: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11

需要

apt install dbus-x11 dbus -y
eval `dbus-launch --sh-syntax`

然后再运行example-client.py

  • 重回终端1,可以看到最后多了一行打印“Hello from example-client.py!”
dbus@Ubuntu18:~/dbus$ python3 example-service.py 
Running example service.
Usage:
python example-service.py &
python example-client.py
python example-async-client.py
python example-client.py --exit-service

Hello from example-client.py!
  • 在终端2执行
dbus@Ubuntu18:~/dbus$ python3 example-client.py --exit-service
  • 重回终端1,发现service已经退出

尝试理解

service

  • 创建一个Session D-Bus对象session_bus
  • 定义bus name,用".“隔开的一串字符,这里是"com.example.SampleService”
  • 定义object path(一个bus name下可以有很多object提供不同的功能),用"/“隔开,这里是”/SomeObject",将class SomeObject实例化
  • class SomeObject继承自dbus.service.Object,定义了很多的函数
    • HelloWorld:在service端打印client发来的hello信息,并返回dbus.Array,相当于字符串列表
    • RaiseException:实例化class DemoException,发起异常
    • GetTuple:返回dbus.Struct(元组)
    • GetDict:返回dbus.Dictionary(字典)
    • GetEdition:使得service执行"cat /etc/issue"命令获取系统版本信息,然后返回dbus.Dictionary告知client
    • Exit:使得client可以让service退出mainloop

client

  • 调用service定义的这些接口,打印service的返回信息
  • 加上"–exit-service",触发service quit

signature

  • signature用来描述method和signal所需参数的数量和类型
  • 具体描述看这里

图形化调试

  • 安装d-feet
$ sudo apt install d-feet -y
  • 在Linux系统图像化窗口的终端运行’d-feet’,在弹出的窗口选取"Session Bus",定位到SampleInterface
    在这里插入图片描述

  • 双击"GetEdition",在弹出的窗口点击"Excute",因为不需要输入,可以看到从service获取的信息
    在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值