码:
def get_user_totp_status (user_name = ''):
key_name = 'tfaEnable'
try:
bus = dbus.SystemBus()
except:
print "connect dbus error!"
sys.exit(1)
infopipe_obj = bus.get_object("org.freedesktop.sssd.infopipe", "/org/freedesktop/sssd/infopipe")
ifp = dbus.Interface(infopipe_obj,dbus_interface='org.freedesktop.sssd.infopipe')
print "get user totp status from dbus error!"
result = ifp.GetUserAttr(user_name, [key_name])
user_totp_status = 'True'
if result:
for status in result[key_name]:
user_totp_status = status
return user_totp_status
===========
错误:
dbus.exceptions.DBusException:org.freedesktop.DBus.Error.TimedOut:org.freedesktop.sssd.infopipe的激活超时
========
如果DBUS有问题,则在“ get_object”时将花费很长时间。如何设置超时时间以缩短时间?
最佳答案
get_object
和start_service_by_name
都不支持超时。
作为解决方法,您可以像这样直接在call_blocking
之前使用get_object
:
import dbus
from _dbus_bindings import BUS_DAEMON_IFACE, BUS_DAEMON_NAME, BUS_DAEMON_PATH
bus = dbus.SystemBus()
timeout = 2
flags = 0
bus_name = "org.freedesktop.sssd.infopipe"
try:
bus.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
BUS_DAEMON_IFACE,
'StartServiceByName',
'su', (bus_name, flags), timeout=timeout)
infopipe_obj = bus.get_object("org.freedesktop.sssd.infopipe", "/org/freedesktop/sssd/infopipe")
except dbus.exceptions.DBusException as e:
print("The chosen bus is not available: %s", e)
关于python - Python dbus get_object设置超时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49704350/