3

Here is my ugly little code snippet now:

for i in range(5):
        try:
            self.startTime=time.time()
            self.rawfeed=requests.get(self.feedurl)
            continue
        except ConnectionError:
            print "Got a connection error.  Retrying", i
            time.sleep(i*i) # giving a bit longer wait each time
            pass
if i == 5: raise
self.ResponseTime=time.time()-self.startTime

Two problems here. First my except statement does not seem to recognize "ConnectionError" as an exception (SOLVED, thanks, stackers), but rather complains that it isn't a global variable. And second, and more important, I would really like to know if the error is MY side, or the SERVER's side.

Typically I want to retry on MY errors, but give up and report on a SERVER error. (By "MY" error, I mean anything other than a server error.)

2
  • remove try, except. detect exceptions. does code give same exception for YOUR side errors and SERVER side errors?(try to reproduce errors) if they are different, catch yours. btw, you suppose to import relevant exception to catch it. Jan 9, 2013 at 13:51
  • 1
    re: your 'i is not a global variable' -- this is a case where you'd want to use the relatively obscure else clause on for loops: docs.python.org/release/1.5/tut/node23.html
    – bgporter
    Jan 9, 2013 at 13:52

2 Answers 2

3

Taking a look at the requests source code, ConnectionError is only raised if it retries the maximum amount of times (MaxRetryError).

except MaxRetryError, e:
    if not self.config.get('safe_mode', False):
        raise ConnectionError(e)

It's quite impossible to say whether or not this is caused by your side or the server, as so many issues on both sides could cause this exact same error. For example, if the server's HTTP daemon is not running, your connection attempts will time out. The exact same thing will happen if you block them in your router or if your router drops the connection for some other reason.

Exception HTTPError, though, which you are not catching, is much more likely to be a server error that you can die on. ConnectionError is somewhat likely, but not at all guaranteed, to be yourself.

Your second issue is that you are not properly importing these classes. Either change your except to except requests.ConnectionError, or do from requests import ConnectionError, HTTPError.

1
  • Thanks. That is kind of what I was thinking. Looks like if I fix my try block I will be able to do what I need. To a first approximation at least. Jan 9, 2013 at 13:54
2

Like any name in Python, you need to import ConnectionError before you can use it. In this case, it is available via the already-imported requests namespace, so you can do except requests.ConnectionError:.

As regards your main query, unfortunately this does not really make sense. There's the entire Internet between you and the server: at which point would you define something as being "your" error? Your wifi access point? Your router? Your modem? Your ISP? Some upstream proxy somewhere between you and the destination? Of course, there's no way to tell where the error occurs, without using a utility like traceroute.

2
  • Ah, yes, I can see that is unclear. "My" error would be anything other than an error reported by the server. I want to know if the SERVER acted in some sort of erroneous manner, or something else. Jan 9, 2013 at 13:50
  • And thanks for the "try/except" assistance. I am still wrapping my head around that. Blame ten years of Perl before I began pythoning. Jan 9, 2013 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.