如何通过图形化 sudo 在 python 中获得 root 权限?
我的 python 程序的一部分需要管理员访问权限。如何使用类似于 gksudo 命令的 GUI 弹出窗口获得 root 权限?
我只需要程序的一小部分的 root 权限,因此最好只拥有特定功能的权限。
我希望能够做类似的事情:
gksudo(my_func, 'description of why password is needed')
Part of my python program needs administrator access. How can I gain root privileges using a GUI popup similar to the gksudo
command?
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')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gksudo 可用于启动以管理员权限运行的程序。应用程序中需要以 root 身份运行的部分必须能够作为单独的进程从命令行调用。如果您需要两者之间进行某种形式的通信,您可以使用套接字或监视文件等。
gksudo can be used to launch programs running with administrator privileges. The part of your application that needs to run as root, must be able to be invoked as a separate process from the command line. If you need some form of communication between the two, you could use sockets or watch for a file, etc.
这里有两个选择:
您需要将需要 root 权限的程序部分制作为一个单独的文件,然后像这样执行该文件:
这将显示密码提示并以 root 身份运行 that_file.py
您还可以要求一个程序从一开始就以 root 身份运行,只需让程序用户从一开始就在命令行中键入“gksudo python your_program.py”,如果您的程序通常从菜单启动,这显然不是最好的主意。
You have two options here:
You will need to make the part of the program that requires root privileges a separate file and then execute the file like this:
which will bring up the password prompt and run that_file.py as root
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.