Pyinstaller加密打包应用的示例代码

所属分类: 脚本专栏 / python 阅读数: 1365
收藏 0 赞 0 分享

pyinstaller是一个非常简单的打包python的py文件的库。用起来就几条命令就够了,

官方文档:pyinstaller

代码混淆

使用https://pyob.oxyry.com/ 进行代码的混淆(找不到什么可用的离线混淆库)

image.png

抓取真实api后

def obfuscation(py_file, save_path):
 print("读取文件:", py_file)
 with open(py_file, "r", encoding="utf-8") as f:
  py_content = f.read()

 print("进行混淆中...")
 url = "https://pyob.oxyry.com/obfuscate"
 headers = {
  "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
  "Referer": "http://pyob.oxyry.com/",
  "content-type": "application/json",
  "cookie": "_ga=GA1.2.1306886713.1588752647; _gid=GA1.2.46944674.1588899118"
 }
 data = json.dumps({
  "append_source": "false",
  "preserve": "",
  "remove_docstrings": "true",
  "rename_default_parameters": "false",
  "rename_nondefault_parameters": "true",
  "source": py_content
 })
 result = json.loads(requests.post(url, data=data, headers=headers).text)["dest"]
 result = "# cython: language_level=3\n" + result
 print("混淆成功...")

 with open(save_path, "w", encoding="utf-8") as f:
  f.write(result)
 print("混淆文件已写入{}\n".format(save_path))
 
if __name__ == '__main__':
 obfuscation("my.py", "../混淆/my.py")
 obfuscation("approach.py", "../混淆/approach.py")

编译pyd

build_pyd.py

from distutils.core import setup
from Cython.Build import cythonize

setup(
 name='any words.....',
 ext_modules=cythonize(["my.py","approach.py" ])
)

执行打包

import json
import os
# 清理旧pyd文件
import uuid
import requests
def clearPyd():
 for file in os.listdir():
  if ".pyd" in file:
   print("删除.pyd:", file)
   os.remove(file)
 print("***********************************************************************")
# 构建pyd文件
def buildPyd():
 os.system("python build_pyd.py build_ext --inplace")
# 重命名pyd文件
def renamePyd():
 print("***********************************************************************")
 for file in os.listdir():
  if ".pyd" in file:
   print("重新命名pyd:", file)
   os.rename(file, file[:file.find(".")] + ".pyd")
 for file in os.listdir():
  if ".c" in file:
   print("删除.c文件:", file)
   os.remove(file)
 print("***********************************************************************")
# 执行打包
def pyinstaller(key, ico):
 os.system("pyinstaller -F --key {} -i {} main.py".format(key, ico))
# 删除bulid和spec文件
def clearBuildAndSpec():
 import shutil
 shutil.rmtree('build')
 print("删除bulid文件夹")
 os.remove("main.spec")
 print("删除spec文件")
if __name__ == '__main__':
 clearPyd() # 清理旧pyd文件
 buildPyd() # 构建pyd文件
 renamePyd() # 重命名pyd文件
 pyinstaller(uuid.uuid4()[0:16], "1.ico") # 执行打包
 clearPyd() # 清理pyd文件
 clearBuildAndSpec() # 删除bulid和spec文件
更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多