python源码安全--将py编译成so
2021/4/11 20:27:50
本文主要是介绍python源码安全--将py编译成so,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. 应用场景
Python是一种面向对象的解释型计算机程序设计语言,具有丰富和强大的库,使用其开发产品快速高效。
python的解释特性是将py编译为独有的二进制编码pyc文件,然后对pyc中的指令进行解释执行,但是pyc的反编译却非常简单,可直接反编译为源码,当需要将产品发布到外部环境的时候,源码的保护尤为重要.
2. 准备工作
环境是可为linux/centos,我Windows10本地是Bash on Ubuntu on Windows,用起来很方便,命令行打bash即进入命令行
思路是先将py转换为c代码,然后编译c为so文件
所以要安装以下内容
python 安装:cython
pip install cython
linux 安装:python-devel,gcc
yum install python-devel yum install gcc
3. 代码编译
3.1 第一种办法:
执行命令:cython test.py
结果:会在同一目录下面生成test.c文件
执行命令: gcc -c -fPIC -I /usr/include/python2.7 test.c
结果: 在同一目录下面生成test.o文件
执行命令: gcc -shared test.o -c test.so
结果: 在同一目录下面生成test.so文件
最后,生成的test.so文件就是需要的文件
3.2 第二种办法:
[setup.py] from distutils.core import setup from Cython.Build import cythonize setup( name = "test", ext_modules = cythonize("test.py") )
- 执行命令: python setup.py build_ext --inplace
第二种办法是对单独文件进行编译,下面介绍一种批量的办法:
#-*- coding:utf-8 -*-_ import os import re from distutils.core import Extension, setup from Cython.Build import cythonize from Cython.Compiler import Options # __file__ 含有魔术变量的应当排除,Cython虽有个编译参数,但只能设置静态。 exclude_so = ['__init__.py', 'run.py'] sources = 'backend' extensions = [] remove_files = [] for source,dirnames,files in os.walk(sources): for dirpath, foldernames, filenames in os.walk(source): if 'test' in dirpath: break; for filename in filter(lambda x: re.match(r'.*[.]py$', x), filenames): file_path = os.path.join(dirpath, filename) if filename not in exclude_so: extensions.append( Extension(file_path[:-3].replace('/', '.'), [file_path], extra_compile_args = ["-Os", "-g0"], extra_link_args = ["-Wl,--strip-all"])) remove_files.append(file_path[:-3]+'.py') remove_files.append(file_path[:-3]+'.pyc') Options.docstrings = False compiler_directives = {'optimize.unpack_method_calls': False, 'always_allow_keywords': True} setup( # cythonize的exclude全路径匹配,不灵活,不如在上一步排除。 ext_modules = cythonize(extensions, exclude = None, nthreads = 20, quiet = True, build_dir = './build', language_level = 2, compiler_directives = compiler_directives)) # 删除py和pyc文件 for remove_file in remove_files: if os.path.exists(remove_file): os.remove(remove_file)
- 执行命令: python setup.py build_ext --inplace
- 结果:最后生成.so文件,删除中间结果。
平时主要使用第一种方法,第二种暂未尝试
这篇关于python源码安全--将py编译成so的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-18Python编程基础
- 2024-10-18Python开发基础:变量与类型
- 2024-10-16Python基础教程:从变量与类型开始
- 2024-10-16Python编程基础与进阶
- 2024-10-16Python编程基础教程
- 2024-10-16Python基础入门指南
- 2024-10-11Python编程基础与实践
- 2024-10-11Python编程基础
- 2024-10-11Python编程基础教程
- 2024-10-11使用Python和FMP API来分析一下绩优股的股息走势。