【问题标题】:pyinstaller error: OSError: [WinError 6] The handle is invalidpyinstaller错误:OSError:[WinError 6]句柄无效
【发布时间】:2021-11-24 05:32:39
【问题描述】:

这个文件使用终端命令netsh wlan show profiles获取wifi密码 我之前使用 pyinstaller 创建了一些 .exe,它们运行良好。

代码:

import subprocess
import time
import sys
import re

command_output = subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True).stdout.decode()

profile_names = (re.findall("All User Profile     : (.*)\r", command_output))

wifi_list = []

if len(profile_names) != 0:
    for name in profile_names:
        wifi_profile = {}
        profile_info = subprocess.run(["netsh", "wlan", "show", "profile", name], capture_output = True).stdout.decode()
        if re.search("Security key           : Absent", profile_info):
            continue
        else:
            wifi_profile["ssid"] = name
            profile_info_pass = subprocess.run(["netsh", "wlan", "show", "profile", name, "key=clear"], capture_output = True).stdout.decode()
            password = re.search("Key Content            : (.*)\r", profile_info_pass)
            if password == None:
                wifi_profile["password"] = None
            else:
                wifi_profile["password"] = password[1]
            wifi_list.append(wifi_profile)

for x in range(len(wifi_list)):
    print(wifi_list[x])

time.sleep(5)
print("No more WiFi Profiles Found")
time.sleep(3)
sys.exit()

这是我在运行 .exe 时遇到的错误:

Traceback (most recent call last):
  File "GetWiFiPassWord.py", line 6, in <module>
  File "subprocess.py", line 453, in run
  File "subprocess.py", line 709, in __init__
  File "subprocess.py", line 1006, in _get_handles
OSError: [WinError 6] The handle is invalid

【问题讨论】:

    标签: python windows subprocess pyinstaller


    【解决方案1】:

    这个错误显然是抛出的,因为:

    subprocess.py 中的第 1117 行是:p2cread = _winapi.GetStdHandle(_winapi.STD_INPUT_HANDLE)

    服务进程没有与之关联的 STDIN (TBC)。

    可以通过向popen 提供文件或空设备作为标准输入参数来避免此问题。

    Python 3.x 中,您可以简单地传递 stdin=subprocess.DEVNULL。 例如

    subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.DEVNULL)
    

    Python 2.x中,你需要让一个filehandler为null,然后通过 要打开的:

    devnull = open(os.devnull, 'wb')
    subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=devnull)  
    

    参考:OSError: (WinError 6) The handle is Invalid


    在你的问题中:

    subprocess.run(["netsh", "wlan", "show", "profiles"], capture_output = True, stdin=subprocess.DEVNULL).stdout.decode()
    

    【讨论】:

      【解决方案2】:

      我在 pyinstaller 上遇到了同样的问题:

      PyInstaller: 4.5.1
      Python: 3.9.6
      Platform: Windows-10-10.0.19042-SP0
      

      感谢Behdad Abdollahi Moghadam。我通过将stdinstder 添加到subprocess.check_output 解决了我的问题

      subprocess.check_output("C:\Program Files (x86)\xxx.exe", stdin=subprocess.DEVNULL, stderr=subprocess.STDOUT)
      

      【讨论】:

        猜你喜欢
        • 2022-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-18
        • 2017-02-27
        • 1970-01-01
        相关资源
        最近更新 更多