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

yipeiwu_com6年前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设计】。

相关文章

python类的实例化问题解决

python类的实例化问题解决

类的实例化问题解决 运行结果: line 21, in <module> s=speaker('ken',10,'aaa') TypeError: __init__(...

python list数据等间隔抽取并新建list存储的例子

原始数据如下: ['e3cd', 'e547', 'e63d', '0ffd', 'e39b', 'e539', 'e5be', '0dd2', 'e3d6', 'e52e', 'e...

Python的re模块正则表达式操作

这个模块提供了与 Perl 相似l的正则表达式匹配操作。Unicode字符串也同样适用。 正则表达式使用反斜杠" \ "来代表特殊形式或用作转义字符,这里跟Python的语法冲突,因此...

python 捕获 shell/bash 脚本的输出结果实例

#!/usr/bin/python ## get subprocess module import subprocess   ## call date command ##...

在Python的Flask框架中使用模版的入门教程

 概述 如果你已经阅读过上一个章节,那么你应该已经完成了充分的准备工作并且创建了一个很简单的具有如下文件结构的Web应用:   microblog  &nb...