如何通过图形化sudo在python中获得root特权? [英] How to gain root privileges in python via a graphical sudo?
问题描述
我的python程序的一部分需要管理员访问权限.如何使用类似于gksudo
命令的GUI弹出窗口获得root特权?
Part of my python program needs administrator access. How can I gain root privileges using a GUI popup similar to the gksudo
command?
我只需要一小部分程序的root特权,因此最好只具有特定功能的特权.
I only need root privileges for a small part of my program so it would be preferable to only have the privileges for a particular function.
我希望能够做类似的事情:
I'm hoping to be able to do something like:
gksudo(my_func, 'description of why password is needed')
推荐答案
您在这里有两个选择:
您需要将需要root特权的程序部分划分为一个单独的文件,然后像这样执行该文件:
You will need to make the part of the program that requires root privileges a separate file and then execute the file like this:
>>> import subprocess
>>> subprocess.call(['gksudo','python that_file.py'])
这将显示密码提示并以root用户身份运行that_file.py
which will bring up the password prompt and run that_file.py as root
您还可能要求程序从一开始就以root身份运行,并且仅从一开始就在命令行中让程序用户键入"gksudo python your_program.py",这显然不是最好的主意.通常是从菜单启动的.
You could also require that a program be run as root from the start and just have the program user type "gksudo python your_program.py" in the command-line from the start which is obviously not the best idea if your program is normally launched from a menu.
这篇关于如何通过图形化sudo在python中获得root特权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!