python - 如何以编程方式阻止应用程序读取计算机文件?
问题描述
在像 Python 这样的脚本语言中,如何阻止应用程序读取计算机文件,尤其是敏感的系统级文件?
编辑:要清楚,我的意思是将该应用程序设置为无权访问计算机文件,即使在脚本运行完成后(设置权限)也是如此。
解决方案
您应该以非特权用户身份运行您的 python 脚本并在验证访问权限后读取文件
# check readability of the path
if os.access("file.txt", os.R_OK):
# open txt file as file
with open("file.tx") as file:
return file.read()
推荐阅读
- python - 带有Docker的GCP VM Django项目,无法访问端口
- azure - 如何在 azure API 管理之前使用 Azure 流量管理器
- c# - System.Drawing.dll 中发生“System.ArgumentException”,但未在用户代码中处理
- javascript - 得到一个'无法设置属性'onclick'错误'
- mysql - MYSQL:创建唯一索引以避免在许多条目显示0时出现重复条目
- c++ - 测量 C++ 中并发代码使用的内存消耗
- c++ - 从未排序的数组中删除重复项
- python - 在 Django 中使用旧数据库进行身份验证登录
- python - Clarifai:“格式错误或无效的请求”
- ef-core-2.0 - 实体框架核心重用一组包含语句