gpt4 book ai didi

Python 和 Pyinstaller : include binary application in code

转载 作者:太空宇宙 更新时间:2023-11-03 17:09:08 25 4
gpt4 key购买 nike

我有一个小应用程序。为了保护我使用 c:/hwid.exe。应用程序返回 PC 的 HWID。示例代码:

def HWID_LIC():
try:
if hashlib.md5(open('c:\hwid.exe', 'rb').read()).hexdigest() != 'bca173dc':
sys.exit(1)
out = os.popen("c:\hwid.exe").read().strip()
if out not in lic:
sys.exit(1)
except:
sys.exit(1)

HWID_LIC()

我想将 hwid.exe 包含在我的应用程序中,并且需要将解决方案全部包含在一个文件中。在 Python 中可以做到这一点吗? hwid.exe 有 30 kb。

最佳答案

这是可能的。您需要在sys._MEIPASS中搜索您的hwid.exe

import sys
import os
import hashlib

def HWID_LIC():
if os.path.isfile('hwid.exe'):
print('file found in .')
else:
print('file not found in .')

if os.path.isfile(sys._MEIPASS + os.sep + 'hwid.exe'):
print('file found in sys._MEIPASS')
else:
print('file not found')
print(hashlib.md5(open(sys._MEIPASS + os.sep + 'hwid.exe', 'rb').read()).hexdigest())


if __name__ == '__main__':
HWID_LIC()

示例规范文件:

# -*- mode: python -*-
a = Analysis(['test.py'],
pathex=[],
hiddenimports=[],
runtime_hooks=None)
import platform
if platform.system().find("Windows")>= 0:
a.datas = [i for i in a.datas if i[0].find('Include') < 0]
a.binaries = [x for x in a.binaries if not x[0].startswith("scipy")]
a.binaries = [x for x in a.binaries if not x[0].startswith("numpy")]
a.datas += [('hwid.exe','.\\hwid.exe','DATA'),]
pyz = PYZ(a.pure)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='test.exe',
debug=False,
strip=None,
clean=True,
upx=False,
console=True)

并创建 onefile exe

pyinstaller t_onefile.spec

执行 test.exe 时您会看到:

j:\tmp>test.exe
file not found in .
file found in sys._MEIPASS
754222d71581010a45732c471437ecf7

关于Python 和 Pyinstaller : include binary application in code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34292731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com