2012-12-06 79 views
2

我在交互式python shell中嘗試了以下代碼,並在代碼行3中得到了錯誤,使用D-Feet我發現路徑和接口存在於總線中,並且使用命令dbus-send我能夠獲取設備,請參閱此消息的結尾。爲什麼不在Python中使用這段代碼? PS:我使用Ubuntu 12.04,在Ubuntu 11中也嘗試過,同樣的問題。爲什麼無法在Python中使用dbus調用org.freedesktop.NetworkManager中的方法?

代碼:

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" 
    ] 
的代碼

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 10

這只是一個例子,我想知道爲什麼不工作,如果我改變線3(注意第一個參數的DBus名):

obj = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/NetworkManager') 

不發生錯誤,但是在這個接口中GetDevices方法不存在。

回答

5

在命令行中例如你要求的系統總線:

dbus-send --system ... 

在Python代碼,你問的會話總線:

bus = dbus.SessionBus() 

如果你嘗試請求通過系統總線,我想你會發現它的工作原理:

>>> import dbus 
>>> bus = dbus.SystemBus() 
>>> obj = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager') 
>>> t = dbus.Interface(obj, "org.freedesktop.NetworkManager") 
>>> t.GetDevices() 
dbus.Array([dbus.ObjectPath('/org/freedesktop/NetworkManager/Devices/0'), dbus.ObjectPath('/org/freedesktop/NetworkManager/Devices/1')], signature=dbus.Signature('o')) 
>>> 
+0

謝謝!你是對的,我是新的使用dbus,不要以前得到這個,非常感謝! – Msum

相關問題