开发者

Need help with python exception handling

开发者 https://www.devze.com 2023-02-11 01:15 出处:网络
Here is the code I\'m currently working with: url = locations[state][\'url\'] headers = {\'User-Agent\':\'Firefox/3.6.13\'}

Here is the code I'm currently working with:

url = locations[state]['url']
headers = {'User-Agent':'Firefox/3.6.13'}
req = urllib.request.Request(url, headers=headers)
try:
    resp = urllib.request.urlopen(req)
except:
    print('Caught error, trying again...')
    print('This should be handled better, I\'m sorry')
    time.sleep(2)
    resp = urllib.request.urlopen(req)

The problem I'm getting, and thus the exception I actually care about is this will occasionally happen when making a request:

URLError: <urlopen error [Errno 104] Connection reset by peer>

That's not the exact error, I think that may be for python 2.x's urllib/urllib2 and I'm on python3, which I think is urllib.error.URLError iirc. Anyway though, I know I can do except URLError and it should work (though I wonder if I need to do urllib.error.URLError instead since that's what it reports on mine), but how do I test to make sure it's because of a 104. I would like it to keep retrying to request until it gets it, or at least try it a specified number of times, how would I开发者_如何学运维 do that most elegantly?

From what I can find the error 104 is because my local router can't handle the request and freaks out, I'm guessing because it can't handle the requests so quickly? If anyone has any further insight as to what causes that, that too would be helpful, but I'm not too concerned.


Take a look at http://docs.python.org/py3k/library/urllib.error.html

Once you look at the reason attribute on the exception you should be able to determine:

  1. Is the reason attribute a socket.error instance?
  2. If so, is the value of that error a 2-tuple, with the first element corresponding to errno.ECONNRESET?


First, there's no reason to use urllib in new code, urllib2 is recommended instead.

As I understood, you want to retry only if you've got error 104. This is how it's usually done in python:

import time, urllib.request, urllib2.error
RETRY_DELAY = 2

# build req here
# ...

for x in range(10): # Always limit number of retries
  try:
    resp = urllib.request.urlopen(req)
  except urllib.error.URLError:
    if e.reason[0] == 104: # Will throw TypeError if error is local, but we probably don't care
      time.sleep(RETRY_DELAY)
    else:
      raise # re-raise any other error
  else:
    break # We've got resp sucessfully, stop iteration
0

精彩评论

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