pythondbus

Python DBus unknown method or interface


I'm following a study guide for Bluetooth development for Linux found here, and am new to DBus programming.

The guide uses Python to interact with the System DBus as follows.

#!/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)

However, I get the following error when the interface.Get method is called.

dbus.exceptions.DBusException: org.freedesktop.DBus.Error.UnknownMethod: Unknown method Get or interface org.freedesktop.Dbus.Properties.

I can run the the Get method for the org.freedesktop.Dbus.Properties interface of the org.freedesktop.hostname1 object using d-feet, which returns the hostname as expected. I have also tried running the python script as root but still get the same error.

I took a pcap capture for Wireshark using dbus-monitor and compared the d-feet request to the python dbus request and I am not able to see what the issue is. Both request are sent to the same destination and appear to be structured the same with the same relevant details except the array of dictionary elements seem to be in a different order (not sure if that matters).

d-feet request:

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

Response to 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 request:

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

Response to 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.

What am I missing here? Is it possible to send some other requests to discover what methods are available for the selected interface and/or to verify it's the correct interface?

Thanks.


Solution

  • The issue was resolved by correcting a typo as pointed out by ukBaz.

    org.freedesktop.Dbus.Properties --> org.freedesktop.DBus.Properties

    Now working as expected.