我正在嘗試編寫一個小型網絡掃描器來玩。子進程模塊沒有任何屬性STARTUPINFO
代碼:
# 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")
你是否得到這個工作?我有同樣的問題。 –