I/O操作,我需要幫助,請我試圖解決這個問題,因爲3天前沒有成功,並在蟒蛇即時通訊新代碼,請做修改並解釋:ValueError異常:在關閉的文件錯誤
這是代碼它和arp掃描器通過scapy它採用scapy掃描的結果並將結果的掃描(ip和macaddress)保存到linux機器中的txt文件中
然後運行一個名爲(doublepulsar-detection-script )通過這個命令:os.system(「python detect_doublepulsar_smb.py --file last.txt」)
last.txt =文件包含Scapy的掃描結果的
但是當掃描完成我得到這個錯誤:
Traceback (most recent call last): File "test.py", line 116, in lena = int(raw_input("Enter Number : ")) ValueError: I/O operation on closed file
這是全碼:
logging.basicConfig(format='%(asctime)s %(levelname)-5s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
logger = logging.getLogger(__name__)
def long2net(arg):
if (arg <= 0 or arg >= 0xFFFFFFFF):
raise ValueError("illegal netmask value", hex(arg))
return 32 - int(round(math.log(0xFFFFFFFF - arg, 2)))
def to_CIDR_notation(bytes_network, bytes_netmask):
network = scapy.utils.ltoa(bytes_network)
netmask = long2net(bytes_netmask)
net = "%s/%s" % (network, netmask)
if netmask < 16:
logger.warn("%s is too big. skipping" % net)
return None
return net
def scan_and_print_neighbors(net, interface, timeout=1):
logger.info("arping %s on %s" % (net, interface))
try:
ans, unans = scapy.layers.l2.arping(net, iface=interface, timeout=timeout, verbose=True)
for s, r in ans.res:
line = r.sprintf("%Ether.src% %ARP.psrc%")
try:
hostname = socket.gethostbyaddr(r.psrc)
line += " " + hostname[0]
except socket.herror:
# failed to resolve
pass
logger.info(line)
except socket.error as e:
if e.errno == errno.EPERM: # Operation not permitted
logger.error("%s. Did you run as root?", e.strerror)
else:
raise
while True:
lena = int(raw_input("Enter Number : "))
print(lena)
if __name__ == "__main__":
import sys
import os
import time
sys.stdout = open('textout.txt', 'w+')
if lena == 1:
for network, netmask, _, interface, address in scapy.config.conf.route.routes:
# skip loopback network and default gw
if network == 0 or interface == 'lo' or address == '127.0.0.1' or address == '0.0.0.0':
continue
if netmask <= 0 or netmask == 0xFFFFFFFF:
continue
net = to_CIDR_notation(network, netmask)
if interface != scapy.config.conf.iface:
# see http://trac.secdev.org/scapy/ticket/537
logger.warn("skipping %s because scapy currently doesn't support arping on non-primary network interfaces", net)
if net:
scan_and_print_neighbors(net, interface)
sys.stdout.close()
os.system("awk 'NR > 2 {print $2}' textout.txt > last.txt")
os.system("python detect_doublepulsar_smb.py --file last.txt")
elif lena == 3 :
print("bye Bye")
break
請修復您的縮進。 – Steampunkery
很難說如果沒有看到正確縮進的完整代碼,但它可能與'sys.stdout.close()'行有關。如果你關閉標準輸出,那麼你怎麼能在之後打印東西? – Kevin
哇,這段代碼有更多的錯誤,而不僅僅是異常。您需要將所有主例程放在if __name__ ==「__main __」:「中,而不僅僅是其中的一部分。你能回到原來的代碼作者問他們嗎? –