0

我不时出现一些异常,但想不出原因。

这是一个片段:

    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) 被尝试覆盖。

错误是:

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] 的信息,但不知道如何防止它。

任何帮助都将不胜感激。

4

1 回答 1

0

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

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

BadStatusLine异常被定义httplib,你except urllib2.URLError根本没有抓住它。您可能应该改进您的异常处理逻辑,例如:

except (httplib.HTTPException, urllib2.URLError) as err:
    ...
于 2012-06-18T10:12:30.307 回答