【问题标题】:Python: Continue looping after exceptionPython:异常后继续循环
【发布时间】:2012-07-03 08:12:59
【问题描述】:

我有以下脚本(如下)。这将返回 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"

谢谢,

Mhibbin

【问题讨论】:

  • 您是否尝试过使用 try...except 块捕获异常,然后发出警告并继续?
  • @Alex Wilson,我又玩了一个游戏……然后改变了我的问题……这是你的意思吗?

标签: python exception loops


【解决方案1】:

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

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"

【讨论】:

  • 我在发布问题后得出了同样的结论。 :-) 感谢您确认我所做的事情。
  • for line in f 在这里可能比 while 循环更好。
  • 当然,如果您在剥离换行符 (\n) 之前针对空字符串 (line == '':) 测试每一行,那么您可以处理源文件中的空行而不会过早退出。
  • @H.邓禄普:if line:if not line: continue
  • @H.Dunlop,谢谢我有大约 100 个要测试的 url 列表(用于内部网络连接目的(它们是带有 webUI 的设备)。我们想每 60 秒检查一次。是否可能为了加快这个过程......我可以使用这个脚本为每个主机生成一个小脚本(或者这对性能不利)
【解决方案2】:

您需要处理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()

【讨论】:

  • 感谢您的回答。我已经用我同时找到的可能的解决方案编辑了我的问题。
【解决方案3】:

您可以尝试像这样在 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-01
    • 2023-01-13
    • 2020-07-01
    • 2018-03-20
    • 2015-08-19
    • 2021-03-25
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多