python - 如何在linux中严格使用python以管理员权限读取二进制文件

标签 python linux ubuntu

所以我试图读取一个需要“sudo”的配置文件,但我试图仅使用 python 而不使用终端来读取其内容。当我使用下面的代码时,出现如下错误:

with open('/etc/motion/motion.conf','rb') as file:
 data = file.readlines()

IOError: [Errno 13] Permission denied: '/etc/motion/motion.conf'

有什么我可以尝试用 python 严格读取文件内容吗?

最佳答案

您应该从 shell 脚本调用您的 python 文件,将该脚本的所有者更改为 root,然后设置该脚本的 SUID 位。用户将能够运行该脚本,但该脚本的所有者将是 root:

当然,您必须意识到此类事情可能涉及的安全问题,但对于本地使用,用一个简单的脚本做一件事,这是可以的。

关于python - 如何在linux中严格使用python以管理员权限读取二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679169/

相关文章:

c - 程序在屏幕上显示奇怪的字符

python - Python 中的链表实现错误

python - Pandas DataFrame 具有图形节点级别和方阵边

linux - 在 Linux 调度程序中检测长时间等待

PostgreSQL 9.6 : pgadmin4 is not opening on localhost:5050 (Ubuntu 16. 04)

ubuntu - 构建 Caffe 时找不到 protoc 错误

python - 如何使用 glXChooseFBConfig 和 ctypes 模块初始化 OpenGL?

python - 使用 python 和 h5py 更新 hdf5 中的键值

c - Linux 内核 - kthread_stop 总是返回 -EINTR?

python - 我可以从之前运行的 python 脚本中读取代码吗