开发者

Retrying on Connection Reset

开发者 https://www.devze.com 2023-02-02 08:40 出处:网络
I\'m using urllib.request to download files from the internet. However sometimes I 开发者_StackOverflow中文版get Connection Reset by Peer and I want to retry.

I'm using urllib.request to download files from the internet. However sometimes I 开发者_StackOverflow中文版get Connection Reset by Peer and I want to retry.

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

Any suggestions?


Well this part is not needed, first of all.

except Exception as e:
    raise

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

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. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消