Python DBus未知方法或接口
我正在按照一个关于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中捕获了数据包,并比较了d-feet的请求和Python的dbus请求,但我找不到问题所在。两个请求都是发送到同一个目标,看起来结构也一样,相关的细节也相同,只是字典元素的数组顺序似乎不同(不确定这是否重要)。
d-feet请求:
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-feet的响应:
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.
我在这里漏掉了什么吗?有没有可能发送其他请求来发现所选接口可用的方法,或者验证它是否是正确的接口?
谢谢。
1 个回答
1
这个问题是通过修正一个拼写错误解决的,感谢ukBaz的指正。
原本写的是org.freedesktop.Dbus.Properties,正确的应该是org.freedesktop.DBus.Properties。
现在一切都正常了。