重试连接重置 [英] Retrying on Connection Reset

查看:43
本文介绍了重试连接重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 urllib.request 从互联网下载文件.但是有时我会收到 Connection Reset by Peer 并且我想重试.

I'm using urllib.request to download files from the internet. However sometimes I get Connection Reset by Peer and I want to retry.

我尝试了以下操作,但似乎 e.errno 包含 socket error 而不是实际的 errno:

I tried the following, but it seems that e.errno contains socket error and not an actual errno:

while True:
  try:
    filename, headers = urllib.request.urlretrieve(url)
    break
  except IOError as e:
    if e.errno != errno.ECONNRESET:
      raise
  except Exception as e:
    raise

有什么建议吗?

推荐答案

嗯,这部分不需要,首先.

Well this part is not needed, first of all.

except Exception as e:
    raise

IOError 的参数是错误的类型(套接字错误)和给它的错误.反过来,此错误不是原始错误,而是该错误在 args 中,因此...

And the arguments of the IOError is the type of error (socket error) and the error given to it. This error, in turn, is not the original error, but that error is in the args, so...

except IOError as e:
    if e.args[1].args[0].errno != errno.ECONNRESET:
       raise

应该可以.我没有可以在我身上重置的服务器,因此我无法 100% 对其进行测试,但它适用于 ECONNREFUSED.:-)

Should work. I don't have a server that will reset on me, so I can't test it 100% But it works with ECONNREFUSED. :-)

这篇关于重试连接重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆