2

我有这个代码:

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

但有时我会打印出这样的异常:

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

这些由except urllib2.URLError. 他们应该通过if isinstance(error.reason, socket.timeout)测试,但他们没有。

所以我想知道instance这个异常是什么。我怎样才能做到这一点?

4

2 回答 2

1

type()函数返回对象的类型。

在这种情况下,您可以使用print type(error.reason)来诊断对象的类型。reason

于 2013-01-16T09:17:48.260 回答
0

用这个:

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
于 2013-01-16T09:45:35.687 回答