子进程模块没有属性STARTUPINFO

时间:2016-12-07 02:28:34

标签: python

我正在尝试编写一个小型网络扫描仪来玩。

代码:

# Configure subprocess to hide the console window

info = subprocess.STARTUPINFO()

info.dwFlags |= subprocess.STARTF_USESHOWWINDOW

info.wShowWindow = subprocess.SW_HIDE

这是造成问题的块。在运行时,我收到以下错误:

Enter a network address in CIDR format(ex.192.168.1.0/24): 192.168.1.1
Traceback (most recent call last):
  File "scannerMock.py", line 17, in <module>
    info = subprocess.STARTUPINFO()
AttributeError: module 'subprocess' has no attribute 'STARTUPINFO'

我在网上环顾四周并重新安装了子进程模块无济于事,不知道为什么会这样做?

完整代码:

# Import modules
import subprocess
import ipaddress
import os
import sys
# Prompt the user to input a network address
net_addr = input("Enter a network address in CIDR format(ex.192.168.1.0/24): ")

# Create the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts on that network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
#startupinfo = None
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# For each IP address in the subnet,
# run the ping command with subprocess.popen interface
for i in range(len(all_hosts)):
    output = subprocess.Popen(['ping', '-c', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]

    if "Destination host unreachable" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    elif "Request timed out" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    else:
        print(str(all_hosts[i]), "is Online")

2 个答案:

答案 0 :(得分:1)

您是否有机会使用Linux?我在Ubuntu下遇到了同样的问题。我发现Python 3文档说STARTUPINFO类仅在Windows下可用。参见subprocess.STARTUPINFO

答案 1 :(得分:0)

你可能会以某种方式摧毁subprocess。请print subprocess.__ file__,我打赌它不是来自核心。