这篇文章主要介绍了如何基于python实现脚本加密,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 |
from pathlib import Pathimport python_minifierimport compileallimport sys def get_save_path(from_dir, to_dir, p): ori_dir = str(from_dir.resolve()) _p = str(p.resolve()) return to_dir.joinpath(_p.split(ori_dir)[-1][1:])def mix_pyminifier(from_dir, to_dir): for _p in from_dir.rglob('*.py'): with _p.open(encoding='utf-8') as read_fp: res = python_minifier.minify(read_fp.read()) _to_p = get_save_path(from_dir, to_dir, _p) Path(_to_p.parent).mkdir(exist_ok=True, parents=True) with _to_p.open('w', encoding='utf-8') as write_fp: write_fp.write(res) def py2pyc(from_dir, to_dir): compileall.compile_dir(str(from_dir.resolve()), maxlevels=20, force=True)def pyc2py(from_dir, to_dir): passif __name__ == '__main__': origin_dir = Path(sys.argv[1]) target_dir = Path(sys.argv[2]) pymini_dir = target_dir.joinpath('pymini') pyc_dir = target_dir.joinpath('pyc_dir') py_dir = target_dir.joinpath('py_dir') mix_pyminifier(origin_dir, target_dir) # py2pyc(origin_dir, pyc_dir) # pyc2py(pyc_dir, pyc_dir)
以上就是本文的全部内容,希望对大家的学习有所帮助。
原文地址:https://www.linuxprobe.com/linux-python-compileall.html