zoukankan      html  css  js  c++  java
  • Cython保护Python代码

    注:.pyc也有一定的保护性,容易被反编译出源码...

    项目发布时,为防止源码泄露,需要对源码进行一定的保护机制,本文使用Cython将.py文件转为.so进行保护。这一方法,虽仍能被反编译,但难度会比较大。另外,Cython是Python的超集。

    自行安装Cython

    1,创建complie.py文件

    from Cython.Build import cythonize
    from Cython.Distutils import build_ext
    from setuptools import setup
    from setuptools.extension import Extension

    setup(
    ext_modules=cythonize(
    [
    Extension('project.*', ['project/*.py']),
    Extension('project.api.*', ['project/api/*.py']),
    Extension('project.api.bizs.*', ['project/api/bizs/*.py']),
    Extension('project.api.data.export*', ['project/api/data/export/*.py']),
    Extension('project.api.exceptions.*', ['project/api/exceptions/*.py']),
    # 需要保护的.py文件目录写在此处
    ],
    build_dir='build',
    compiler_directives=dict(
    always_allow_keywords=True, language_level=3
    )
    ),
    cmdclass=dict(
    build_ext=build_ext
    )
    )

    language_level代表python版本,python2就写2

    2,运行命令:python compile.py build_ext --inplace

     将会在各目录生成每个.py文件的.so文件,和一个build文件夹。.py文件已经被保护了,.so文件之间可以相互调用。

    部署时删除项目中.py文件、build文件夹。

    如果项目中使用了celery,注意不要编译celery代码,否则celery将无法使用。

    贴一个shell脚本,配合上段代码使用。

    #!/bin/bash
    
    # 清除缓存目录
    find . -type d -name __pycache__ | xargs rm -rf
    
    # 编译代码
    python3 -m venv env
    sh env/bin/activate
    python3 compile.py build_ext --inplace
    if [ $? -ne 0 ]; then
        exit 1
    fi
    
    
    # 更改celery文件
    mv ./project/api/tasks/cele/__init__.py ./project/api/tasks/cele/__init__.py.bak
    mv ./project/api/tasks/cele/base.py ./project/api/tasks/cele/base.py.bak
    mv ./project/api/tasks/cele/export.py ./project/api/tasks/cele/export.py.bak
    mv ./project/api/tasks/__init__.py ./project/api/tasks/__init__.py.bak
    mv ./project/api/tasks/dispatch_subdomain.py ./project/api/tasks/dispatch_subdomain.py.bak
    mv ./project/api/tasks/recognize_area.py ./project/api/tasks/recognize_area.py.bak
    
    
    # 将.so文件改名
    find ./project  -name '*.so' | awk -F '.cpython-36m-x86_64-linux-gnu' '{print "mv "$0" "$1$2}' | sh
    
    # 删除.py文件
    find ./project  -name '*.py' | xargs rm -f
    
    mv ./project/api/tasks/cele/__init__.py.bak ./project/api/tasks/cele/__init__.py
    mv ./project/api/tasks/cele/base.py.bak ./project/api/tasks/cele/base.py
    mv ./project/api/tasks/cele/export.py.bak ./project/api/tasks/cele/export.py
    mv ./project/api/tasks/__init__.py.bak ./project/api/tasks/__init__.py
    mv ./project/api/tasks/dispatch_subdomain.py.bak ./project/api/tasks/dispatch_subdomain.py
    mv ./project/api/tasks/recognize_area.py.bak ./project/api/tasks/recognize_area.py
    
    # 清除不需要的文件
    rm -rf build
    rm -f .gitignore
    rm -f compile.py
    rm -f build.sh

    END!

  • 相关阅读:
    ueditor PHP版本使用方法
    PHP三维优先级运算
    navicate for mysql中的sql存放位置和备份
    wamp配置步骤
    phpeclipse xdebug 配置配置 -摘自网络
    xls 和 xml 数据 排序 绑定 -原创
    XSLT教程 比较全的 -摘自网络
    XPath在asp.net中查询XML -摘自网络
    windows上zend server安装 报The server encountered an internal error or misconfiguration and was unable to complete your request -解决方法 摘自网络
    开源项目 配置管理软件推荐
  • 原文地址:https://www.cnblogs.com/ldy-miss/p/11649318.html
Copyright © 2011-2022 走看看