python dbus库

文章介绍了如何在Python中使用dbus库(如python3自带的dbus和第三方库pydbus)进行进程间通信。示例代码展示了如何获取系统服务的对象并调用其接口,特别提到了当使用dbus-python库时可能遇到的问题以及如何通过dbus.PROPERTIES_IFACE获取对象属性。此外,还提到了其他Python的DBus绑定选项,如dasbus和dbus-next,并推荐了dbus查看工具dbus-viewer和qdbusviewer。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python dbus库

cat dbus-cmd.py
python打印引入模块的文件路径
https://fiime.cn/blog/241146

#!/usr/bin/env python3

import sys

import dbus.mainloop.glib

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

bus = dbus.SystemBus()
print(dbus.__file__)
o = bus.get_object('net.reactivated.Fprint', '/net/reactivated/Fprint/Manager', introspect=False)
print(o)
o = o.GetDefaultDevice()
print(o)
o = bus.get_object('net.reactivated.Fprint', o, introspect=False)
print(o)
o = dbus.Interface(o, 'net.reactivated.Fprint.Device')
print(o)
#print(o.RunCmd(sys.argv[1]))

输出

print(dbus.__file__)
/usr/lib/python3/dist-packages/dbus/__init__.py

方法1

python3 自带了dbus包,所以我们直接应用,例子:

import dbus
session_bus = dbus.SessionBus()

proxy = session_bus.get_object('com.deepin.filemanager.daemon', '/com/deepin/filemanager')  

proxy_dev = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Introspectable')

方法2

使用第三方pydbus,先安装

pip3 install pydbus

例子:获取授权状态

from pydbus import SystemBus

bus = SystemBus()

proxy = bus.get(bus_name='com.deepin.license', object_path='/com/deepin/license/Info')

print(proxy.AuthorizationState)

我采用的是pydbus这个库,操作简单。

dbus-python这个库使用途中获取不到属性了,还没有仔细研究。这个问题解决:
https://dbus.freedesktop.org/doc/dbus-specification.html#standard-interfaces-properties

import dbus

bus = dbus.SystemBus()

proxy = bus.get_object(bus_name='com.deepin.license', object_path='/com/deepin/license/Info')
_iface = dbus.Interface(proxy, dbus.PROPERTIES_IFACE)
s1 = _iface.Get('com.deepin.license.Info', 'AuthorizationState')

print(s1)

这个方法是真的不好用
官网
https://github.com/freedesktop/dbus-python

https://pydbus.readthedocs.io/en/latest/installation.html

https://wiki.python.org/moin/DbusExamples

11

The D-Bus library is a messaging library used by various desktop environments (GNOME, KDE, etc) for interprocess communication.

There are multiple Python bindings for DBus:

GDbus and QtDbus are wrappers over the C/C++ APIs of GLib and Qt

pydbus is a modern, pythonic API with the goal of being as invisible a layer between the exported API and its user as possible

dasbus is a Python3-only alternative to pydbus with additional features and better flexibility

dbus-python is a legacy API, built with a deprecated dbus-glib library, and involving a lot of type-guessing (despite “explicit is better than implicit” and “resist the temptation to guess”).

txdbus is a native Python implementation of the D-Bus protocol for the Twisted networking framework.

dbus-next is a native Python library for D-Bus with support for multiple IO backends suitable for GUI develeopment and cross platform projects.

See also: DBusBindings on Freedesktop wiki.

The dbus-viewer and qdbusviewer programs let you browse through the services and interfaces available on your system.

pydbus
For more information see pydbus’s Readme.

Introspection

11

即使安装了dbus,python3也无法导入dbus
即使安装了dbus,python3也无法导入dbus
您可能会遇到此错误,因为apt-get安装的模块的路径不在您的sys.path中。您可以尝试以下解决方案:

import sys
sys.path.insert(0, "/usr/lib/python3/dist-packages")
import dbus
### 如何在 Python 中使用 DBus 方法 在 Python 中使用 DBus 方法可以通过 `pydbus` 或者更底层的 `dbus-python` 实现。以下是关于如何通过这些调用 DBus 接口的具体说明。 #### 使用 `dbus-python` 调用 DBus 方法 下面是一个完整的例子,展示如何连接到 Session Bus 并调用远程对象的方法: ```python import dbus # 创建一个SessionBus实例 bus = dbus.SessionBus() # 获取指定的服务名称对应的对象 obj = bus.get_object('org.fmddlmyy.Test', '/TestObj') # 定义接口并获取其代理 iface = dbus.Interface(obj, 'org.fmddlmyy.Test.Basic') # 调用 Add 方法,并传递参数 100 和 999 sum_value = iface.Add(100, 999) print(sum_value) # 输出计算结果 ``` 上述代码展示了如何通过 `dbus.SessionBus()` 来访问名为 `org.fmddlmyy.Test` 的服务及其 `/TestObj` 对象上的方法 `Add`[^1]。 #### 使用 `pydbus` 简化操作 如果希望简化代码结构,可以考虑使用更高层次封装的 `pydbus` 。它提供了更加直观的方式来进行交互: ```python from pydbus import SessionBus # 建立会话总线连接 bus = SessionBus() # 获取目标服务和路径的对象 test_service = bus.get('org.fmddlmyy.Test', '/TestObj') # 直接调用方法 result = test_service.Add(100, 999) print(result) ``` 此方式隐藏了许多复杂的细节处理过程,使开发者能够专注于业务逻辑而非协议层面的内容[^3]。 #### 错误处理机制 无论采用哪种,在实际开发过程中都需要加入异常捕获来增强程序健壮性。例如当无法找到对应服务时抛出错误等情况均需妥善管理: ```python try: sum_value = iface.Add(100, 999) except Exception as e: print(f"An error occurred while calling method: {e}") ``` 以上片段确保即使发生问题也能得到适当反馈而不是直接崩溃退出[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值