Skip to main content
 首页 » 编程设计

python之在 python 中通过 mod_wsgi 连接 Dbus

2025年05月04日11lovecherry


我正在尝试编写允许通过网页发送 dbus 命令(到 Amarok)的小型应用程序。
我正在使用 python + mod_wsgi,因为我需要使用与 Amarok 相同的用户来运行脚本。

当我通过普通 shell 连接到 Amarok 时,它可以工作。但是通过脚本连接后,出现以下错误:

DBusException: org.freedesktop.DBus.Error.Spawn.ExecFailed: /usr/bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed. 

连接Amarok的代码:

import dbus 
conn = dbus.SessionBus().get_object('org.kde.amarok','/Player') 

你知道我应该怎么做才能通过 dbus 连接到 Amarok 吗?
非常感谢您的帮助!

更新: 我会给你一些关于配置的额外信息:

httpd.conf:

LoadModule wsgi_module  modules/mod_wsgi.so 
WSGIScriptAlias /amarok /var/www/amarok-python/config.wsgi 
WSGIDaemonProcess l user=wojtas group=wojtas processes=1 
WSGIProcessGroup l 


配置.WSGI:

import sys 
path='/var/www/amarok-python' 
if path not in sys.path: 
    sys.path.append(path) 
import index 
application=index.application 

应用程序代码(index.py):

import dbus 
from os import getuid 
def connect(): 
        conn = dbus.SessionBus().get_object('org.kde.amarok','/Player') 
        conn.Start() 
        return conn 
def application(environ,start_response): 
    status= '200 OK' 
    connection=connect() 
    output=str(getuid()) 
    response_headers= [('Content-type','text/html'), ('Content-Length', str(len(output)))] 
    start_response (status,response_headers) 
    return [output] 

请您参考如下方法:

问题是我没有设置连接 dbus 所需的 DISPLAY 变量。 您可以查看此网页上的小教程: http://blog.wojtass.pl/tutorial-control-amarok-remotely-through-web-browser/