Cython保护Python代码
2024-10-01 10:46:21
注:.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 ]; then
exit
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!
最新文章
- MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析
- 【原】移动web页面给用户发送邮件的方法 (邮件含文本、图片、链接)
- ThinkPHP3.2.3--Linux服务器首页文件index.php路径配置问题
- Openvswitch原理与代码分析(8): 修改Openvswitch代码添加自定义action
- codeforces 713A A. Sonya and Queries(状态压缩)
- C++ Tempatet之模板模型
- Keil工程文件的建立、设置与目标文件的获得
- #ifdef __cplusplus extern c #endif 的作用
- 解析一下rtmp协议比较难懂的地方
- 武汉科技大学ACM :1003: 零起点学算法67——统计字母数字等个数
- poj--1517
- android模拟器 一个错误:X Error of failed request: BadRequest (invalid request code or no such operation)
- javascript 正则介绍
- 三、ASP.NET MVC Controller 控制器(二:IController控制器的创建过程)
- scp命令和sftp命令
- chrome插件推荐
- (转)Centos搭建FTP服务器
- asp.net mvc 接入最新支付宝支付+退款 alipay-sdk-NET-20170615110549
- Linux lvs-NAT模式配置详解
- Let's Encrypt泛域名SSL证书申请
热门文章
- NOSQL数据库之 REDIS
- PHP1.9--数组
- loj6402 校门外的树(dp,多项式求逆)
- Vue中利用$emit实现子组件向父组件通信
- 通过button将form表单的数据提交到action层
- javax.el.PropertyNotFoundException: Property 'studentAge' not found on type com.hs.model.StudentModel
- springmvc配置不拦截静态资源
- 类的加载classload和类对象的生成
- day37 02-Hibernate二级缓存:二级缓存的散装数据
- CSS-DOM的小知识(二)