python 源代码保护 之 xx.py -> xx.so
前情提要
之前由于项目的需要,需要我们将一部分“关键代码”隐藏起来。
虽然Python 先天支持 将源代码 编译后 生成 xxx.pyc 文件,但是破解起来相当容易 -_-!!
于是搜罗到了另外一种方法,将关键的代码文件/库 转换成 .so ,从而将其保护起来。
使用 Cython 保护代码 ( 测试环境为:Ubuntu16.04 - LTS)
① 准备工作
1. 安装 cython
pip install cython
2. 安装 python-dev
sudo apt-get install python-dev
3. 安装gcc
sudo apt-get install gcc
② 新建setup.py,内容如下
from distutils.core import setup
from Cython.Build import cythonize setup(ext_modules = cythonize(["Train_predict.py"]))
③ 运行脚本 (注: 需要在Python 文件的同目录下运行)
python setup.py build_ext
④ 运行脚本后,在当前目录会生成一个 /build 子目录

打开后,可以看到 .so 文件已经生成:

将 .so 文件拷贝到原来 .py 文件的目录后,删除 .py文件;测试: 在没有修改任何调用程序源码的情况下,在pythoncharm中已经无法找到 “Train_predict.py” 中函数的定义,但是却可以正常的调用这个函数。

OK, 我们已经成功地将“关键代码” 隐藏起来; 任务完成!!
最后,感谢大家的观看,欢迎留言讨论哦 :)
参考:
http://fengzheng369.blog.163.com/blog/static/7522097920161253407914
http://www.cnblogs.com/ke10/p/py2so.html
补充
今天在网上查了下资料,发现可以一次编译多个.py文件为 .so
from distutils.core import setup
from Cython.Build import cythonize #setup(
#ext_modules = cythonize("Train_predict.py") #) setup(
ext_modules = cythonize(["SCIPinterface.py", "normalize.py"])
)
我们注意到可以用 list 作为cythonize的参数来传入,于是,这里尝试着传递了2个新的Python源程序文件 ("SCIPinterface.py", "normalize.py")
生成对应的 .so 文件以及terminal如下所示:

Done ~~
叒 一次的补充
现在我们已经可以生成变异后的 .so 文件了,但是每次编译生成的 临时文件(例如.c)然后处理起来很麻烦!!, 于是发现了另一个好东西,这里搬运过来。 也感谢原文的作者为大家打来的福音!!
集成编译
最新代码github:https://github.com/ArvinMei/py2so.git
做了以下内容:
1.文件夹编译
2.删除编译出的.c文件
3.删除编译的temp文件夹
#-* -coding: UTF- -* -
__author__ = 'Arvin' """
执行前提:
系统安装python-devel 和 gcc
Python安装cython
编译整个当前目录:
python py-setup.py
编译某个文件夹:
python py-setup.py BigoModel
生成结果:
目录 build 下
生成完成后:
启动文件还需要py/pyc担当,须将启动的py/pyc拷贝到编译目录并删除so文件
""" import sys, os, shutil, time
from distutils.core import setup
from Cython.Build import cythonize starttime = time.time()
currdir = os.path.abspath('.')
parentpath = sys.argv[] if len(sys.argv)> else ""
setupfile= os.path.join(os.path.abspath('.'), __file__)
build_dir = "build"
build_tmp_dir = build_dir + "/temp" def getpy(basepath=os.path.abspath('.'), parentpath='', name='', excepts=(), copyOther=False,delC=False):
"""
获取py文件的路径
:param basepath: 根路径
:param parentpath: 父路径
:param name: 文件/夹
:param excepts: 排除文件
:param copy: 是否copy其他文件
:return: py文件的迭代器
"""
fullpath = os.path.join(basepath, parentpath, name)
for fname in os.listdir(fullpath):
ffile = os.path.join(fullpath, fname)
#print basepath, parentpath, name,file
if os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.'):
for f in getpy(basepath, os.path.join(parentpath, name), fname, excepts, copyOther, delC):
yield f
elif os.path.isfile(ffile):
ext = os.path.splitext(fname)[]
if ext == ".c":
if delC and os.stat(ffile).st_mtime > starttime:
os.remove(ffile)
elif ffile not in excepts and os.path.splitext(fname)[] not in('.pyc', '.pyx'):
if os.path.splitext(fname)[] in('.py', '.pyx') and not fname.startswith('__'):
yield os.path.join(parentpath, name, fname)
elif copyOther:
dstdir = os.path.join(basepath, build_dir, parentpath, name)
if not os.path.isdir(dstdir): os.makedirs(dstdir)
shutil.copyfile(ffile, os.path.join(dstdir, fname))
else:
pass #获取py列表
module_list = list(getpy(basepath=currdir,parentpath=parentpath, excepts=(setupfile)))
try:
setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
except Exception, ex:
print "error! ", ex.message
else:
module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), copyOther=True)) module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile), delC=True))
if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir) print "complate! time:", time.time()-starttime, 's'
这样,我们就可以和麻烦的临时文件 say goodbye 啦 ~~
python 源代码保护 之 xx.py -> xx.so的更多相关文章
- Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式
Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式 目录 Pycharm使用技巧(转载) Python第一天 安装 shell ...
- SyntaxError: Non-ASCII character '\xe5' in file D:/pcode/xx.py on line 21, but no encoding declared
from selenium import webdriver from datetime import * import time starttime = datetime.now() print ( ...
- Python源代码剖析笔记3-Python运行原理初探
Python源代码剖析笔记3-Python执行原理初探 本文简书地址:http://www.jianshu.com/p/03af86845c95 之前写了几篇源代码剖析笔记,然而慢慢觉得没有从一个宏观 ...
- Python 2.7 cython cythonize py 编译成 pyd 谈谈那些坑
Python 2.7 cython cythonize py 编译成 pyd 谈谈那些坑 前言 基于 python27 的 pyc 很容易被反编译,于是想到了pyd,加速运行,安全保护 必要准备 安装 ...
- 《python源代码剖析》笔记 Python的编译结果
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.python的运行过程 1)对python源码进行编译.产生字节码 2)将编译结果交给p ...
- 《python源代码剖析》笔记 Python虚拟机框架
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1. Python虚拟机会从编译得到的PyCodeObject对象中依次读入每一条字节码指令 ...
- 如何打包发布加密的 Python 源代码
这里介绍一种使用 PyInstaller 和 PyArmor 来发布加密 Python 源代码的方式,能够达到以下目的 把所有 Python 源代码打包成为可执行文件,客户不需要 Python 就可以 ...
- tomcat 启动参数 Xms, Xmx, XX:MaxNewSize, XX:PermSize, -XX:MaxPermSize, Djava.awt.headless
在 tomcat/bin/catalina.sh 的 第一行#!/bin/sh 下添加 JAVA_OPTS="-server -Xms512m -Xmx1024m -XX:MaxNewSiz ...
- -XX:PermSize -XX:MaxPermSize 永久区参数设置
-XX:PermSize -XX:MaxPermSize –设置永久区的初始空间和最大空间 -XX:PermSize 设置持久代(perm gen)初始值,物理内存的1/64 -XX:MaxPe ...
随机推荐
- JS中关于把函数作为另一函数的参数的几点小总结
//JS中关于把函数作为函数的参数来传递的问题的小总结//第一,最简单的形式无参函数,直接形式函数的函数名放到括号中,再在执行部分这个函数即可.//当然调用时要穿另一个真正的定义好的函数/*funct ...
- spinlock变量没有初始化
http://blog.csdn.net/longwang155069/article/details/52224284
- mac svn: E210004: Number is larger than maximum
SVN服务器IP地址发现改变,在Eclipse中的SVN资源库中执行Relocate重定位时发生错误: mac svn: E210004: Number is larger than maximum ...
- Split功能的思想实现
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- MPLS基础一(上)
在上图中,关于PC-A和PC-B之间互相访问的过程中 1.所有三层网络设备形成源和目的的路由条目 2.PC-A发出报文,source-IP为A,destination-IP为B 3.R1收到报文后,根 ...
- Android Kill Process
/********************************************************************** * Android Kill Process * 说明: ...
- 文件的copy
def mycopy(src_filename, dst_filename): try: fr = open(src_filename, "rb") try: try: fw = ...
- JS格式化数字(每三位加逗号)
function toThousands(num) { var num = (num || 0).toString(), result = ''; //判断是否带小数点 if (num.split(' ...
- for循环使用append问题
append添加到元素的时候,已存在的元素是移动而不是复制,使用了遍历,所以第一次是新增,后面都是移动前面新增的,最后当然就出现了只有最后一项有东西.解决办法:1.不要遍历,使用jQuery的类选择器 ...
- 每天一个linux命令:【转载】pwd命令
Linux中用 pwd 命令来查看”当前工作目录“的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的确切位置. ...