[英]How to Execute Python Script Using pkexec
For a project I am writing a PyQt5 widget for ifconfig
on Linux.对于一个项目,我正在为 Linux 上的
ifconfig
编写 PyQt5 小部件。 Some parts of my code require admin privilege.我的代码的某些部分需要管理员权限。 For example, shutting down an interface.
例如,关闭一个接口。
This is a GUI application and users must enter their sudo password using a GUI.这是一个 GUI 应用程序,用户必须使用 GUI 输入他们的 sudo 密码。 I have searched online and found
pkexec
that allows an authorized user to execute program as another user.我在网上搜索,发现
pkexec
允许授权用户以另一个用户身份执行程序。 So, I wrote the .policy
action file required.因此,我编写了所需的
.policy
操作文件。 I have never used polkit before, I might have missed something.我以前从未使用过 polkit,我可能错过了一些东西。
I can execute my program with no errors using python3 ifconfig_Logic.py
我可以使用
python3 ifconfig_Logic.py
地执行我的程序
but if I try executing my program using pkexec python3 ~/Desktop/GUI/ifconfig_Logic.py
The message on the authentication window is但是如果我尝试使用
pkexec python3 ~/Desktop/GUI/ifconfig_Logic.py
执行我的程序,则身份验证 window 上的消息是
Shouldn't the message be Authentication is required to run the ifconfigLogic instead of Authentication is required to install this project ?消息不应该是Authentication is required to run ifconfigLogic而不是Authentication is required to install this project吗?
Also, I get the following error另外,我收到以下错误
Traceback (most recent call last):
File "/home/grandino/Desktop/GUI/ifconfig_Logic.py", line 9, in <module>
baseUIClass, baseUIWidget = uic.loadUiType("ifconfig.ui")
File "/usr/lib/python3/dist-packages/PyQt5/uic/__init__.py", line 199, in loadUiType
winfo = compiler.UICompiler().compileUi(uifile, code_string, from_imports,
File "/usr/lib/python3/dist-packages/PyQt5/uic/Compiler/compiler.py", line 111, in compileUi
w = self.parse(input_stream, resource_suffix)
File "/usr/lib/python3/dist-packages/PyQt5/uic/uiparser.py", line 1020, in parse
document = parse(filename)
File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 1202, in parse
tree.parse(source, parser)
File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 584, in parse
source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'ifconfig.ui'
ifconfig.ui
is the ui file that ifconfig_Logic.py
uses to import the interface. ifconfig.ui
是ifconfig_Logic.py
用来导入接口的 ui 文件。 I thought this issue is beacase ifconfig.ui
is not added in the policy so I added it.我认为这个问题是因为
ifconfig.ui
没有添加到策略中,所以我添加了它。
Here is ifconfig_Logic.py
这是
ifconfig_Logic.py
from PyQt5 import QtCore, QtGui, QtWidgets, QtWidgets
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QProcess, Qt
from PyQt5 import uic
from PyQt5 import QtGui
import sys
baseUIClass, baseUIWidget = uic.loadUiType("ifconfig.ui")
class ifconfig_Logic(baseUIWidget,baseUIClass):
process = QProcess()
send_output = pyqtSignal(str)
def __init__(self, parent = None):
super(ifconfig_Logic,self).__init__(parent)
self.setupUi(self)
self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = ifconfig_Logic(None)
ui.show()
sys.exit(app.exec_())
Policy file政策文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<action id="com.ifconfigLogic">
<description>Run ifconfigLogic</description>
<message>Authentication is required to run the ifconfigLogic</message>
<defaults>
<allow_any>auth_admin_keep</allow_any>
<allow_inactive>auth_admin_keep</allow_inactive>
<allow_active>auth_admin_keep</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path">/home/grandino/Desktop/GUI/ifconfig</annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
</action>
</policyconfig>
How can I fix this issue?我该如何解决这个问题? Why do files seem missing when I use
pkexec
?为什么我使用
pkexec
时文件似乎丢失了?
It is recommended that you build the absolute paths instead of using relative paths.建议您构建绝对路径而不是使用相对路径。
import os.path
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
ui_filename = os.path.join(CURRENT_DIR, "ifconfig.ui")
baseUIClass, baseUIWidget = uic.loadUiType(ui_filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.