2012-07-03 147 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()