在Windows / Linux上使用python3脚本检查可执行文件是32位还是64位 [英] Checking if an executable is 32-bit or 64-bit with a python3 script on Windows/Linux

查看:390
本文介绍了在Windows / Linux上使用python3脚本检查可执行文件是32位还是64位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Python3编写软件(更具体地说: Python 3.8.1 )。在某个时候,软件需要检查某些任意可执行文件是64位还是32位。经过研究,我发现了以下信息:



检查exe是32位还是64位



在本文中,以下解决方案是提供:

  subprocess.call(['dumpbin','/ HEADERS','test2。 exe','|','find',' machine'])

不幸的是,这在 Python 3.8.1 中不起作用。该帖子将近8年了,其历史可以追溯到 Python 2.x 天。



我如何测试从 Python 3.x 中获得64位?我需要适用于Linux和Windows 10的解决方案。


编辑:

<与Windows相关的注释:

显然是 DumpBin 解决方案(请参阅检查exe是32位还是64位帖子)要求安装Visual Studio。对我来说这是一个禁忌。我的Python3软件应该可以在任何Windows 10计算机上运行。



与Linux相关的注释:

在Linux上,我不知道无需测试PE格式的可执行文件。只需Linux可执行文件就可以了。 )很容易,因为它始终位于标头中的同一位置:

  def is_64bit_elf(filename):
具有open(filename, rb)as f:
返回f.read(5)[-1] == 2

我没有Windows系统,因此无法测试,但这可能在Windows上有效:

  def is_64bit_pe(文件名):
导入win32file
返回win32file.GetBinaryType(文件名)== 6


I'm writing software in Python3 (more specifically: Python 3.8.1). At some point, the software needs to check if some arbitrary executable is 64-bit or 32-bit. After some research, I found the following post:

Checking if an exe is 32 bit or 64 bit

In this post, the following solution is offered:

subprocess.call(['dumpbin', '/HEADERS', 'test2.exe', '|', 'find', '"machine"'])

Unfortunately, this doesn't work in Python 3.8.1. That post is almost 8 years old and dates back to the Python 2.x days.

How can I test for 64-bitness from within Python 3.x? I need a solution for both Linux and Windows 10.

EDITS :
Windows-related note:
Apparently the DumpBin solution (see Checking if an exe is 32 bit or 64 bit post) requires Visual Studio to be installed. That's a no-no for me. My Python3 software should run on any Windows 10 computer.

Linux-related note:
On Linux, I don't neet to test PE format executables. Just Linux executables are fine.

解决方案

Detecting the 64-bitness of ELF binaries (i.e. Linux) is easy, because it's always at the same place in the header:

def is_64bit_elf(filename):
    with open(filename, "rb") as f:
        return f.read(5)[-1] == 2

I don't have a Windows system, so I can't test this, but this might work on Windows:

def is_64bit_pe(filename):
    import win32file
    return win32file.GetBinaryType(filename) == 6

这篇关于在Windows / Linux上使用python3脚本检查可执行文件是32位还是64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆