Python dbus 找不到 org.(gnome|freedesktop).ScreenSaver

Posted

技术标签:

【中文标题】Python dbus 找不到 org.(gnome|freedesktop).ScreenSaver【英文标题】:Python dbus can't find org.(gnome|freedesktop).ScreenSaver 【发布时间】:2011-05-25 11:06:26 【问题描述】:

我正在尝试让 python 与 dbus 和屏幕保护程序进行交互,但无论如何它都坚决拒绝这样做。 Perl 似乎没问题,但我写的是 python,而不是 perl(还)

Python 2.6.6 (r266:84292, Sep 15 2010, 16:22:56) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbus
>>> bus = dbus.SystemBus()
>>> ofn = 'org.freedesktop.ScreenSaver'
>>> bus.get_object(ofn, "/org/freedesktop/ScreenSaver")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/pymodules/python2.6/dbus/bus.py", line 244, in get_object
    follow_name_owner_changes=follow_name_owner_changes)
  File "/usr/lib/pymodules/python2.6/dbus/proxies.py", line 241, in __init__
    self._named_service = conn.activate_name_owner(bus_name)
  File "/usr/lib/pymodules/python2.6/dbus/bus.py", line 183, in activate_name_owner
    self.start_service_by_name(bus_name)
  File "/usr/lib/pymodules/python2.6/dbus/bus.py", line 281, in start_service_by_name
    'su', (bus_name, flags)))
  File "/usr/lib/pymodules/python2.6/dbus/connection.py", line 620, in call_blocking
    message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.ServiceUnknown: The name org.freedesktop.ScreenSaver was not provided by any .service files
>>> 

这是我在 perl 其他地方看到的:

my $cmd = "dbus-monitor --session \"type='signal',interface='org.gnome.ScreenSaver',member='SessionIdleChanged'\"";

open (IN, "$cmd |");

while (<IN>) 
if (m/^\s+boolean true/) 
  #print something
 elsif (m/^\s+boolean false/) 
  #print something else


【问题讨论】:

【参考方案1】:
In [36]: import dbus

In [37]: bus = dbus.SessionBus()

In [38]: bus.get_object('org.gnome.ScreenSaver','/org/gnome/ScreenSaver')
Out[38]: <ProxyObject wrapping <dbus._dbus.SessionBus (session) at 0xa41e38c> :1.24 /org/gnome/ScreenSaver at 0xa4a3b0c>

【讨论】:

啊。 SessionBus 和 SystemBus 的区别。非常感谢。

以上是关于Python dbus 找不到 org.(gnome|freedesktop).ScreenSaver的主要内容,如果未能解决你的问题,请参考以下文章

ofono dbus 内省:找不到方法

dbus-发送到远程机器

推荐使用 pydbus 或 dbus-python 有啥区别?

在python中动态创建DBus信号

在 Python 3 中从 dbus 迁移到 GDbus

为啥不能在 Python 中使用 dbus 调用 org.freedesktop.NetworkManager 中的方法?