2012-07-03 116 views
9

我有以下脚本(如下)。这将返回URL的状态码。它通过文件循环并尝试连接到每个主机。唯一的问题是它在达到异常时显然停止循环。Python:异常后继续循环

我已经尝试了很多事情来把它如何在循环中,但无济于事。有什么想法吗?

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" 

感谢,

MHibbin

+1

您是否尝试过使用try ... except块捕获异常,然后警告并继续? –

+0

@亚历克斯威尔逊,我又玩了一次......并改变了我的问题......这是你的意思吗? – MHibbin

+1

确实如此。做得很好... –

回答

11

您可以处理引发异常的地方。此外,打开文件时使用上下文管理器,它使代码更简单。

with open(hostsFile, 'r') as f: 
    for line in f: 
     line = line.strip() 
     if not line: 
      continue 

     epoch = str(time.time()) 

     try: 
      conn = urllib.urlopen(line) 
      print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n" 
     except IOError: 
      print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n" 
+0

我发布这个问题后,我得出了同样的结论。 :-)感谢您确认我做了什么。 – MHibbin

+3

'在f'中的行可能比while循环更好。 – georg

+0

当然,如果您在剥离换行符(\ n)之前针对空字符串(行=='':)测试每行,那么您可以在不退出的情况下处理源文件中的空行。 – aychedee

4

您需要处理异常的urllib.urlopen(line)提出,这样的事情。

try: 
    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) 
     except IOError: 
      print "Exception occured" 
      pass 
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() 
+0

感谢您的回答。我用我找到的可能解决方案编辑了我的问题。 – MHibbin

0

你可以尝试捕获while循环内的异常,就像这样。

try: 
    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: 
      epoch = time.time() 
      epoch = str(epoch) 
      print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n" 
except IOError: 
    pass 
else: 
    f.close()