我正在制作一个 python 应用程序,它有一个需要 root 权限的方法。从https://www.freedesktop.org/software/polkit/docs/0.105/polkit-apps.html,我找到了示例 2。通过 D-Bus 访问权限,这是下面代码的 python 版本,我执行了它并且我认为输入密码后我可以获得root权限,但我的应用程序仍然“权限被拒绝”。这是我要连接的功能
import dbus
bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.PolicyKit1', '/org/freedesktop/PolicyKit1/Authority')
authority = dbus.Interface(proxy, dbus_interface='org.freedesktop.PolicyKit1.Authority')
system_bus_name = bus.get_unique_name()
subject = ('system-bus-name', {'name' : system_bus_name})
action_id = 'org.freedesktop.policykit.exec'
details = {}
flags = 1 # AllowUserInteraction flag
cancellation_id = '' # No cancellation id
result = authority.CheckAuthorization(subject, action_id, details, flags, cancellation_id)
print result