Skip to main content
 首页 » 编程设计

python 和保护磁盘上的 pyc 文件

2025年05月04日14mfryf

我将 django 的 settings.py 文件设置为 chmod 600,以防止不法分子窥探我的数据库连接信息,但在导入时,python 编译该文件并将 settings.pyc 写为模式 644。它不需要太多侦查坏人从这个编译版本中获取他们需要的信息。我担心我的博客条目处于严重危险之中。

除了显而易见的 os.chmod 之外,人们还使用哪些技术来确保编译后的 Python 在磁盘上的安全?

请您参考如下方法:

你可以直接在python中设置umask。解释器使用这个 umask 来创建 pyc 文件:

import os 
os.umask(077) # Only keep rights for owner 
import test 

验证创建的 test.pyc:

$> ls -l test.py* 
-rw-r--r-- 1 shad users  0 2009-11-29 00:15 test.py 
-rw------- 1 shad users 94 2009-11-29 00:15 test.pyc