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

你可能感兴趣的:(python,开发语言)