Cython编译python为so 代码加密示例

yipeiwu_com5年前Python基础

1. 编译出来的so比网上流传的其他方法小很多。

2. language_level  是python的主版本号,如果python版本是2.x,目前的版本Cython需要人工指定language_level.

3. python setup.py build_ext --inplace  执行脚本

4. 以下是代码片段

from distutils.core import Extension, setup
 
from Cython.Build import cythonize
from Cython.Compiler import Options
 
 
# __file__ 含有魔术变量的应当排除,Cython虽有个编译参数,但只能设置静态。
exclude_so = ['__init__.py', "mixins.py"]
sources = ['core', 'libs']
 
 
extensions = []
for source in sources:
  for dirpath, foldernames, filenames in os.walk(source):
    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"]))
 
 
Options.docstrings = False
compiler_directives = {'optimize.unpack_method_calls': False}
setup( 
    # cythonize的exclude全路径匹配,不灵活,不如在上一步排除。
    ext_modules = cythonize(extensions, exclude = None, nthreads = 20, quiet = True, build_dir = './build',
                language_level = 2 或者3 , compiler_directives = compiler_directives))

以上这篇Cython编译python为so 代码加密示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python3.6简单的操作Mysql数据库的三个实例

安装pymysql 参考:https://github.com/PyMySQL/PyMySQL/ pip install pymsql 实例一 import pymysql # 创建...

python正则表达式中的括号匹配问题

问题: m = re.findall('[0-9]*4[0-9]*', '[4]') 可以匹配到4. m = re.findall('([0-9])*4([0-9])*', '[4]'...

解读python如何实现决策树算法

数据描述 每条数据项储存在列表中,最后一列储存结果 多条数据项形成数据集 data=[[d1,d2,d3...dn,result], [d1,d2,d3...dn,resul...

python实现字符串完美拆分split()的方法

函数:split() 例子 我们想要将以下字符串rule进行拆分。字符串表示的是一个规则,由“…”得到“…”。我们需要将规则中的条件属性与取值分别提取出来,存放在条件属性列表cf_lis...

pandas 把数据写入txt文件每行固定写入一定数量的值方法

pandas 把数据写入txt文件每行固定写入一定数量的值方法

我遇到的情况是:把数据按一定的时间段提出。比如提出每天6:00-8:00的每个数据,可以这样做: # -*-coding: utf-8 -*- import pandas as pd...