https://blog.csdn.net/feijiges/article/details/77932382
https://www.icode9.com/content-1-644421.html
https://www.cnblogs.com/jclian91/p/10289820.html
https://mp.weixin.qq.com/s/TTUFQRQ_DiKktJ5-J8O8Pg
由于windows版本执行加密源码的命令会存在一定的问题,所以以下场景都是在linux环境上做的。
2、无法加密django的数据迁移文件,即migrations目录下的0***_initial.py等
3、批量加密目录下的文件时默认输出是无序的(不是按照原目录结构方式输出)。
pip3 install cython
python setup.py build_ext
# setup.py from distutils.core import setup from Cython.Build import cythonize # 相对路径:task.py和setup.py隶属于同一目录 setup(ext_modules=cythonize('task.py')) # 相对路径:task.py和src在同一目录 #setup(ext_modules=cythonize('src/task.py')) # 绝对路径:task.py和src在同一目录 #setup(ext_modules=cythonize('/var/mycode/src/task.py'))
加密的结果就是在当前目录下生成build文件夹,在linux系统下生成的加密文件是以so结尾的,而在windows下生成的是以pyd结尾的文件。
# setup.py import os # 获取当前文件父目录下的子目录 src_path = [] path = os.path.dirname(os.path.abspath(__file__)) for p in os.listdir(path): p_inner = os.path.join(path, p) if os.path.isdir(p_inner): src_path.append(p_inner) # 递归获取src_path里每个目录里的要编译的py文件 py_files = [os.path.join(path, file_name) for compile_path in src_path for path, _, file_list in os.walk(compile_path) for file_name in file_list if file_name.endswith('.py')] print(py_files) from distutils.core import setup from Cython.Build import cythonize setup(ext_modules=cythonize(py_files))
src_dir = '/home/liuwei/mycode/project' to_encrypt = [os.path.join(path, file_name) for path, _, file_list in os.walk(src_dir) for file_name in file_list if file_name.endswith('.py')] setup(ext_modules=cythonize(to_encrypt))
python setup.py build_ext