Alexandru
Alexandru

Reputation: 25810

Retrying on Connection Reset

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?

Upvotes: 0

Views: 3783

Answers (1)

Lennart Regebro
Lennart Regebro

Reputation: 172249

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

Upvotes: 3

Related Questions