我有以下脚本(如下)。这将返回 URL 的状态代码。它遍历一个文件并尝试连接到每个主机。唯一的问题是它在遇到异常时显然会停止循环。
我已经尝试了很多事情来将它的方式放在一个循环中,但无济于事。有什么想法吗?
import urllib
import sys
import time
hostsFile = "webHosts.txt"
try:
f = file(hostsFile)
while True:
line = f.readline().strip()
epoch = time.time()
epoch = str(epoch)
if len(line) == 0:
break
conn = urllib.urlopen(line)
print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except IOError:
epoch = time.time()
epoch = str(epoch)
print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
sys.exit()
else:
f.close()
编辑:
我在此期间提出了这个问题,有什么问题吗?(我还在学习:p)...
f = file(hostsFile)
while True:
line = f.readline().strip()
epoch = time.time()
epoch = str(epoch)
if len(line) == 0:
break
try:
conn = urllib.urlopen(line)
print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
except IOError:
print epoch + "connection unsuccessful"
谢谢,
米宾