我在交互式 python shell 中尝试了下面的代码,并在代码的第 3 行得到了以下错误,使用 D-Feet 我看到总线中存在路径和接口,并且使用命令 dbus-send 我能够获取设备,请参见此消息的结尾。为什么不在 python 中使用此代码?PS:我用的是 ubuntu 12.04,在 ubuntu 11 也试过,同样的问题。
代码:
import dbus
bus = dbus.SessionBus()
obj = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
t = dbus.Interface(obj, "org.freedesktop.NetworkManager")
t.GetDevices()
输入代码第 3 行的输出错误:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/lib/pymodules/python2.7/dbus/bus.py", line 244, in get_object
follow_name_owner_changes=follow_name_owner_changes)
File "/usr/lib/pymodules/python2.7/dbus/proxies.py", line 241, in __init__
self._named_service = conn.activate_name_owner(bus_name)
File "/usr/lib/pymodules/python2.7/dbus/bus.py", line 183, in activate_name_owner
self.start_service_by_name(bus_name)
File "/usr/lib/pymodules/python2.7/dbus/bus.py", line 281, in start_service_by_name
'su', (bus_name, flags)))
File "/usr/lib/pymodules/python2.7/dbus/connection.py", line 630, in call_blocking
message, timeout)
DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.NetworkManager was not provided
by any .service files
有效的 Shell 命令:
dbus-send --system --print-reply --reply-timeout=2000 --type=method_call --dest=org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager.GetDevices
输出:
method return sender=:1.2 -> dest=:1.69 reply_serial=2
array [
object path "/org/freedesktop/NetworkManager/Devices/0"
]
这只是一个例子,我想知道为什么不工作,如果我将第 3 行更改为(注意第一个参数中的 DBus 名称):
obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/NetworkManager')
该错误不会发生,但在此接口中 GetDevices 方法不存在。