Python DBUS SESSION_BUS - X11 dependency

ε祈祈猫儿з 提交于 2019-12-17 19:37:24

问题


I've got running sample python code which is fine in Ubuntu desktop:

import dbus, gobject
from dbus.mainloop.glib import DBusGMainLoop
from dbus.mainloop.glib import threads_init
import subprocess
from subprocess import call

gobject.threads_init()
threads_init()
dbus.mainloop.glib.DBusGMainLoop( set_as_default = True )

p = subprocess.Popen('dbus-launch --sh-syntax', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
call( "export DBUS_SESSION_BUS_ADDRESS" , shell=True )
call( "export DBUS_SESSION_BUS_PID" , shell=True )

bus = dbus.SessionBus()

# get DBUS objects, do other stuff with SESSION_BUS
# in same time we can start more independent processes with this file
# finaly kill the SESSION_BUS process

After success on desktop I moved the code to the server edition which is only with shell. The dbus-launch starts the process, but python dbus.SessionBus() returns error with "/bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed".

Hope there shouldn't be strict dependency between SESSION_BUS and X11 when the process started with "dbus-launch" go up and running with success. The error comes in python.

Best solution will be clean python or linux environment settings, worst but maybe acceptable with some fake or virtual X11 (I was not lucky when I try it)


回答1:


The problem is that you're running the export calls in separate shells. You need to capture the output of dbus-launch, parse the values, and use os.environ to write them to the environment:

p = subprocess.Popen('dbus-launch', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for var in p.stdout:
  sp = var.split('=', 1)
  print sp
  os.environ[sp[0]] = sp[1][:-1]


来源:https://stackoverflow.com/questions/2143785/python-dbus-session-bus-x11-dependency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!