神器Cython

2018年6月14日 0 条评论 1.28k 次阅读 0 人点赞

今天看了下腾讯云的云镜产品,发现里面有个VulnerDetect模块,使用的pyc+so的组合。其中pyc大家想必都了解,至于里面的动态链接库so,云镜使用了cython编译python代码为C代码,有效保护了源码。这也算一种靠谱的产品形态吧。

cython的安装使用很简单:

1.安装

python安装cython:

pip install cython

linux 安装:python-devel,gcc:

yum install gcc python-devel

2.写一个要编译为so的类文件test.py:

#-* -coding: UTF-8 -* -__author__ = 'Mao'
class test:
def say(self):
print 'hello'

3.写一个编译文件setup.py:

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["test.py"]))

4.编译一下,bash中运行:

python setup.py build_ext

运行后会多出一个build文件夹,里面有编译好的test.so文件。拷贝这个文件到test.py所在的目录,现在可以删除test.py咯。

5.调用一个试试,写个main.py:

#-* -coding: UTF-8 -* -__author__ = 'Mao'
from test import test
if __name__ == '__main__':
test().say()

6.运行结果:

可见,运行良好,且真的是ELF动态链接库。。。它娘都认不出来了。。。

管理员

这个人太懒什么东西都没留下