python3打包成so

此博客是演示如何将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

python3打包成so_第1张图片

6.运行测试代码run.py

 

你可能感兴趣的:(信息安全,Python)