python pyd_python项目(.pyd或者.so)代码加密

python项目(.pyd或者.so)代码加密

准备工作

Linux安装:

用到的库

sudo apt-get install python-dev gcc

#windows只需安装下面这两个就可以

pip install pycrypto Cython

在项目文件夹下新建两个文件setup.py, rename.py

加密脚本setup.py的代码如下

# coding:utf-8

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[1] if len(sys.argv) > 1 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('fname',fname)

# print("ffile", ffile)

# print basepath, parentpath, name,file

# 是文件夹 且不以.开头 不是 build ,不是迁移文件

if os.path.isdir(ffile) and fname != build_dir and not fname.startswith('.') and fname != "migrations":

# 循环遍历文件夹

for f in getpy(basepath, os.path.join(parentpath, name), fname, excepts, copyOther, delC):

yield f

elif os.path.isfile(ffile):

# 筛选出 .c 文件

ext = os.path.splitext(fname)[1]

if ext == ".c":

# 显示文件 "ffile" 信息,st_mtime: 最后一次修改的时间。

if delC and os.stat(ffile).st_mtime > starttime:

# 删除 .c 文件

os.remove(ffile)

# 文件不是排除文件 且不是'.pyc' '.pyx'文件

elif ffile not in excepts and os.path.splitext(fname)[1] not in ('.pyc', '.pyx'):

# manage.py文件不编译

if os.path.splitext(fname)[1] in ('.py', '.pyx') and not fname.startswith(

'__') and fname != "manage.py" and fname != "test.py":

# 返回要加密的文件(加到module_list中成为一个列表)

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)

print('dstdir的是',dstdir)

# 复制文件到新文件夹下

print('ffile的是',ffile)

shutil.copyfile(ffile, os.path.join(dstdir, fname))

else:

pass

# 获取py列表

module_list = list(getpy(basepath=currdir, parentpath=parentpath, excepts=(setupfile)))

print(module_list)

try:

setup(ext_modules=cythonize(module_list), script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])

except Exception as e:

print(e)

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))

# shutil.rmtree() 表示递归删除文件夹下的所有子文件夹和子文件

# 删除build_tmp_dir临时文件夹

if os.path.exists(build_tmp_dir):

shutil.rmtree(build_tmp_dir)

print("complate! time:", time.time() - starttime, 's')

方法:将需要加密的代码放到列表里,然后在终端执行 python setup.py

1、如果报错不能找到vcvasall.bat说明没有按照vc++安装之后再执行!

2、如果安装了vc++还是报错的话,这时候需要配置一下路径请看这里

# coding:utf-8

import os

from distutils.core import setup

from Cython.Build import cythonize

import shutil

# filter=[".py",".so",'.c'] linux的过滤条件

filter=[".py",".pyd",'.c'] #设置过滤后的文件类型 当然可以设置多个类型

def all_path(dirname):

result = [] # 所有的文件

for maindir, subdir, file_name_list in os.walk(dirname):

for filename in file_name_list:

apath = os.path.join(maindir, filename)#合并成一个完整路径

ext = os.path.splitext(apath)[1] # 获取文件后缀 [0]获取的是除了文件名以外的内容

# 筛选出.py 和 不是__init__的文件

if ext in filter and os.path.splitext(filename)[0] not in('__init__','manage','test'):

result.append(apath)

else:

pass

return result

def rename(list):

for i in list:

if i.__contains__(".pyd"):

re_name = i.split(".")[0] + '.pyd' #(linux的是.so文件)

print(i.split('.'))

os.rename(i, re_name)

elif i.__contains__(".c"):

os.remove(i)

if __name__ == '__main__':

path = os.getcwd()

list = all_path(path)

print(list)

print(len(list))

rename(list)

再执行一遍rename.py就得到修改后的pyd文件了

至此代码加密完成 加密完的项目在build文件夹中

再次在终端执行python manage.py runserver 运行项目成功

说明加密成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值