我正在遵循here找到的 Linux 蓝牙开发学习指南,并且是 DBus 编程的新手。
本指南使用 Python 与系统 DBus 进行交互,如下所示。
#!/usr/bin/python3
import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.hostname1','/org/freedesktop/hostname1')
interface = dbus.Interface(proxy, 'org.freedesktop.Dbus.Properties')
print("----------------")
hostname = interface.Get('org.freedesktop.hostname1','Hostname')
print("The host name is ",hostname)
但是,当调用interface.Get方法时,出现以下错误。
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method Get or interface org.freedesktop.Dbus.Properties.
我可以使用 d-feet 运行 org.freedesktop.hostname1 对象的 org.freedesktop.Dbus.Properties 接口的 Get 方法,该方法按预期返回主机名。我也尝试过以 root 身份运行 python 脚本,但仍然遇到相同的错误。
我使用 dbus-monitor 对 Wireshark 进行了 pcap 捕获,并将 d-feet 请求与 python dbus 请求进行了比较,我无法看出问题所在。两个请求都发送到相同的目的地,并且看起来结构相同,具有相同的相关细节,除了字典元素数组似乎具有不同的顺序(不确定这是否重要)。
d 脚要求:
Frame 17: 197 bytes on wire (1576 bits), 197 bytes captured (1576 bits)
Encapsulation type: D-Bus (146)
Arrival Time: Mar 13, 2024 07:21:04.048618000 AEDT
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1710274864.048618000 seconds
[Time delta from previous captured frame: 0.001328000 seconds]
[Time delta from previous displayed frame: 0.001328000 seconds]
[Time since reference or first frame: 2.546796000 seconds]
Frame Number: 17
Frame Length: 197 bytes (1576 bits)
Capture Length: 197 bytes (1576 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: dbus]
D-Bus
Endianness: little-endian
Message Type: Method call
Message Flags: 0x00
.... ...0 = No Reply Expected: Expected
.... ..0. = No Auto Start: Start
.... .0.. = Allow Interactive Authorization: Don't allow
Protocol Version: 1
Message Body Length: 45
Message Serial: 623
Header Field Array
Array Length: 134
Dict Entry, 1, Path: /org/freedesktop/hostname1
Field Code: Path (1)
Variant: /org/freedesktop/hostname1
Variant Signature: o
Path: /org/freedesktop/hostname1
Dict Entry, 2, Interface: org.freedesktop.DBus.Properties
Field Code: Interface (2)
Variant: org.freedesktop.DBus.Properties
Variant Signature: s
Interface: org.freedesktop.DBus.Properties
Dict Entry, 6, Destination: :1.171
Field Code: Destination (6)
Variant: :1.171
Variant Signature: s
Destination: :1.171
Dict Entry, 8, Signature: ss
Field Code: Signature (8)
Variant: ss
Variant Signature: g
Signature: ss
Dict Entry, 3, Member: Get
Field Code: Member (3)
Variant: Get
Variant Signature: s
Member: Get
Dict Entry, 7, Sender: :1.83
Field Code: Sender (7)
Variant: :1.83
Variant Signature: s
Sender: :1.83
Body
String: org.freedesktop.hostname1
String: Hostname
对d脚的回应:
Frame 18: 86 bytes on wire (688 bits), 86 bytes captured (688 bits)
Encapsulation type: D-Bus (146)
Arrival Time: Mar 13, 2024 07:21:04.048994000 AEDT
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1710274864.048994000 seconds
[Time delta from previous captured frame: 0.000376000 seconds]
[Time delta from previous displayed frame: 0.000376000 seconds]
[Time since reference or first frame: 2.547172000 seconds]
Frame Number: 18
Frame Length: 86 bytes (688 bits)
Capture Length: 86 bytes (688 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: dbus]
D-Bus
Endianness: little-endian
Message Type: Method reply
Message Flags: 0x01
.... ...1 = No Reply Expected: Not expected
.... ..0. = No Auto Start: Start
.... .0.. = Allow Interactive Authorization: Don't allow
Protocol Version: 1
Message Body Length: 22
Message Serial: 10
Header Field Array
Array Length: 47
Dict Entry, 5, Reply serial: 623
Field Code: Reply serial (5)
Variant: 623
Variant Signature: u
Reply serial: 623
Dict Entry, 6, Destination: :1.83
Field Code: Destination (6)
Variant: :1.83
Variant Signature: s
Destination: :1.83
Dict Entry, 8, Signature: v
Field Code: Signature (8)
Variant: v
Variant Signature: g
Signature: v
Dict Entry, 7, Sender: :1.171
Field Code: Sender (7)
Variant: :1.171
Variant Signature: s
Sender: :1.171
Body
Variant: Lenny-Leonard
Variant Signature: s
String: Lenny-Leonard
Python 请求:
Frame 34: 197 bytes on wire (1576 bits), 197 bytes captured (1576 bits)
Encapsulation type: D-Bus (146)
Arrival Time: Mar 13, 2024 07:21:06.564106000 AEDT
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1710274866.564106000 seconds
[Time delta from previous captured frame: 0.000312000 seconds]
[Time delta from previous displayed frame: 0.000312000 seconds]
[Time since reference or first frame: 5.062284000 seconds]
Frame Number: 34
Frame Length: 197 bytes (1576 bits)
Capture Length: 197 bytes (1576 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: dbus]
D-Bus
Endianness: little-endian
Message Type: Method call
Message Flags: 0x00
.... ...0 = No Reply Expected: Expected
.... ..0. = No Auto Start: Start
.... .0.. = Allow Interactive Authorization: Don't allow
Protocol Version: 1
Message Body Length: 45
Message Serial: 4
Header Field Array
Array Length: 135
Dict Entry, 1, Path: /org/freedesktop/hostname1
Field Code: Path (1)
Variant: /org/freedesktop/hostname1
Variant Signature: o
Path: /org/freedesktop/hostname1
Dict Entry, 6, Destination: :1.171
Field Code: Destination (6)
Variant: :1.171
Variant Signature: s
Destination: :1.171
Dict Entry, 2, Interface: org.freedesktop.Dbus.Properties
Field Code: Interface (2)
Variant: org.freedesktop.Dbus.Properties
Variant Signature: s
Interface: org.freedesktop.Dbus.Properties
Dict Entry, 3, Member: Get
Field Code: Member (3)
Variant: Get
Variant Signature: s
Member: Get
Dict Entry, 8, Signature: ss
Field Code: Signature (8)
Variant: ss
Variant Signature: g
Signature: ss
Dict Entry, 7, Sender: :1.173
Field Code: Sender (7)
Variant: :1.173
Variant Signature: s
Sender: :1.173
Body
String: org.freedesktop.hostname1
String: Hostname
对Python的回应:
Frame 35: 189 bytes on wire (1512 bits), 189 bytes captured (1512 bits)
Encapsulation type: D-Bus (146)
Arrival Time: Mar 13, 2024 07:21:06.564291000 AEDT
[Time shift for this packet: 0.000000000 seconds]
Epoch Time: 1710274866.564291000 seconds
[Time delta from previous captured frame: 0.000185000 seconds]
[Time delta from previous displayed frame: 0.000185000 seconds]
[Time since reference or first frame: 5.062469000 seconds]
Frame Number: 35
Frame Length: 189 bytes (1512 bits)
Capture Length: 189 bytes (1512 bits)
[Frame is marked: False]
[Frame is ignored: False]
[Protocols in frame: dbus]
D-Bus
Endianness: little-endian
Message Type: Error reply
Message Flags: 0x01
.... ...1 = No Reply Expected: Not expected
.... ..0. = No Auto Start: Start
.... .0.. = Allow Interactive Authorization: Don't allow
Protocol Version: 1
Message Body Length: 69
Message Serial: 12
Header Field Array
Array Length: 103
Dict Entry, 5, Reply serial: 4
Field Code: Reply serial (5)
Variant: 4
Variant Signature: u
Reply serial: 4
Dict Entry, 6, Destination: :1.173
Field Code: Destination (6)
Variant: :1.173
Variant Signature: s
Destination: :1.173
Dict Entry, 4, Error name: org.freedesktop.DBus.Error.UnknownMethod
Field Code: Error name (4)
Variant: org.freedesktop.DBus.Error.UnknownMethod
Variant Signature: s
Error name: org.freedesktop.DBus.Error.UnknownMethod
Dict Entry, 8, Signature: s
Field Code: Signature (8)
Variant: s
Variant Signature: g
Signature: s
Dict Entry, 7, Sender: :1.171
Field Code: Sender (7)
Variant: :1.171
Variant Signature: s
Sender: :1.171
Body
String: Unknown method Get or interface org.freedesktop.Dbus.Properties.
我在这里缺少什么? 是否可以发送一些其他请求来发现哪些方法可用于所选接口和/或验证它是正确的接口?
谢谢。
该问题已通过纠正 ukBaz 指出的拼写错误得到解决。
org.freedesktop.Dbus.Properties --> org.freedesktop.DBus.Properties
现在按预期工作。