python中通过mod_wsgi连接Dbus

发布于 2024-09-30 13:50:09 字数 1533 浏览 1 评论 0原文


我正在尝试编写一个小型应用程序,它允许通过网页发送 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.

The code which isconnecting to the Amarok:

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

Do you Know how should I do to connect through dbus to 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


config.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]

I'm trying to write small application which allows to send dbus commands (to Amarok) through web page.
I'm using python + mod_wsgi, because I need to run the script with the same user as Amarok.

When I connect to the Amarok through normal shell, it works. But after connecting through the script, I get the following error:

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

The code which is connecting to the Amarok:

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

Do you know what should I do to connect through dbus to Amarok?
Thanks a lot for help!

Update:
I'll give you some additional information about configuration:

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

config.WSGI:

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

Code of the 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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小情绪 2024-10-07 13:50:09

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

The problem was that I didn't set the DISPLAY variable, which is required for connecting with dbus.
You can check the small tutorial on this webpage:
http://blog.wojtass.pl/tutorial-control-amarok-remotely-through-web-browser/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文