Python是一种面向对象的解释型计算机程序设计语言,具有丰富和强大的库,使用其开发产品快速高效。
python的解释特性是将py编译为独有的二进制编码pyc文件,然后对pyc中的指令进行解释执行,但是pyc的反编译却非常简单,可直接反编译为源码,当需要将产品发布到外部环境的时候,源码的保护尤为重要.
环境是可为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-develyum 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 osimport re from distutils.core import Extension, setup from Cython.Build import cythonizefrom 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 = Falsecompiler_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全部内容,希望文章能够帮你解决python源码安全--将py编译成so所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)