2

I have this code:

try:
    self.client.post(url, data, self.cookies, headers, auth, jsonrpc)
    self.status  = self.client.status
    self.mytime  = self.client.time
    self.text    = self.client.text
    self.length  = len(self.text)
except urllib2.URLError, error:
    print error
    self.exception = True
    self.urrlib2   = True
    if isinstance(error.reason, socket.timeout):
        self.timeout = True

But sometimes I get exceptions printing out like this:

URLError in POST > reason=The read operation timed out > <urlopen error The read operation timed out>

These are handled by the except urllib2.URLError. They should pass the if isinstance(error.reason, socket.timeout) test, but they do not.

So I would like to know what instance this exception is. How can I do this?

blueFast
  • 41,341
  • 63
  • 198
  • 344

2 Answers2

1

The type() function returns the type of an object.

You could use print type(error.reason) to diagnose what type of object reason is in this case.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I have found that it is of instance `ssl.SSLError`. I wonder what this has to do with `urllib2.URLError` – blueFast Jan 16 '13 at 09:47
  • Mmm. I see this http://docs.python.org/2/library/ssl.html. `ssl.SSLError` is of subtype `socket.error` which is of subtype `IOError`. I do not know why it goes through the `except urllib2.URLError, error` code. – blueFast Jan 16 '13 at 09:51
  • @gonvaled: The `urllib2` code catches that exception, then raises `URLError(originalexception)`, and that argument is stored as `.reason`. – Martijn Pieters Jan 16 '13 at 09:55
0

Use this:

import sys

try:
    self.client.post(url, data, self.cookies, headers, auth, jsonrpc)
    self.status  = self.client.status
    self.mytime  = self.client.time
    self.text    = self.client.text
    self.length  = len(self.text)
except urllib2.URLError, error:
    print error
    self.exception = True
    self.urrlib2   = True

    errno, errstr = sys.exc_info()[:2]
    if errno == socket.timeout:
        print "There was a timeout"
        self.timeout = True
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117