此博客是演示如何将python代码打包成so,防止python源码泄露,保证代码安全。以helloworld.py为例,将python打包成so。
1.在/opt/目录下编写需要被编译的代码helloworld.py
#!/usr/bin/env python
# encoding: utf-8
def hello(name):
print("Hello %s!" % name)
2.在/opt/目录下编写Cython编译所需要的setup.py文件
#!/usr/bin/env python
# encoding: utf-8
from distutils.core import setup
from Cython.Build import cythonize
setup(
name = 'Hello world app',
ext_modules = cythonize("/opt/helloworld.py"),
)
如下所示
3.在/opt/目录下执行Cython的编译命令
python3 setup.py build_ext --inplace
多出绿色方框的文件
4. 创建一个调用so文件的pyhton文件,测试编译后的so是否正常工作的代码run.py
#!/usr/bin/env python
# encoding: utf-8
from helloworld import hello
hello('deamon ')
5.删除build文件夹以及helloworld.c
6.运行测试代码run.py
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)