IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 学习D-Bus的python调用 -> 正文阅读

[Python知识库]学习D-Bus的python调用

需要学习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$ 
  • 重回终端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获取的信息
    在这里插入图片描述

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-11-17 12:43:00  更:2021-11-17 12:45:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 1:39:45-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码