Retrying on Connection Reset

旧时模样 提交于 2019-12-12 05:48:29

问题


I'm using urllib.request to download files from the internet. However sometimes I 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?


回答1:


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



来源:https://stackoverflow.com/questions/4605929/retrying-on-connection-reset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!