python dbus库

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值