可以调用pythons urllib2.info()导致异常吗?

时间:2012-06-18 09:59:57

标签: python exception urllib2

我不时会遇到一些例外情况,但却无法想到原因。

这是一个片段:

    try:
        r = urllib2.urlopen(url)
    except urllib2.URLError, e:
        if hasattr(e, 'code'):
            # unauthorized
            print('UA: %s' % url)
        elif hasattr(e, 'reason'):
            print('TO: %s' % url)
            # timeout
    else:
        i = r.info()

        try:
            server = i['server']
        except:
            pass
        else:
            if not 'authenticate' in server:
                print('NA: %s' % url)

我想也许r.info()导致异常,但不确定为什么它会因为r = urllib2.urlopen(url)被try覆盖。

错误是:

Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 551, in __bootstrap_inner
    self.run()
  File "C:\Users\anthony\Scripts\checker.py", line 35, in run
    r = urllib2.urlopen(url)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 418, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1180, in do_open
    r = h.getresponse(buffering=True)
  File "C:\Python27\lib\httplib.py", line 1030, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 371, in _read_status
    raise BadStatusLine(line)
BadStatusLine: ''

  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 400, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 418, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1180, in do_open
    r = h.getresponse(buffering=True)
  File "C:\Python27\lib\httplib.py", line 1030, in getresponse
    response.begin()
  File "C:\Python27\lib\httplib.py", line 407, in begin
    version, status, reason = self._read_status()
  File "C:\Python27\lib\httplib.py", line 365, in _read_status
    line = self.fp.readline()
  File "C:\Python27\lib\socket.py", line 447, in readline
    data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote host

我已经阅读了[Errno 10054]的一些信息,但不知道如何防止它。

任何帮助都会得到满足。

1 个答案:

答案 0 :(得分:0)

  

我想也许r.info()导致异常但不是   确定为什么它会被r = urllib2.urlopen(url)覆盖   尝试。

不。第一个例外与r.info()无关 - 在urllib2.urlopen(url)上引发了异常,正如您在回溯中看到的那样。

BadStatusLine中定义了

httplib例外,而您的except urllib2.URLError根本无法捕获它。你应该改进你的异常处理逻辑,如:

except (httplib.HTTPException, urllib2.URLError) as err:
    ...