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)が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]に関する情報を少し読みましたが、それを防ぐ方法がわかりません。

どんな助けでも適用されるでしょう。

4

1 に答える 1

0

おそらく r.info() が例外を引き起こしていると考えていますが、 r = urllib2.urlopen(url) が try で覆われているため、なぜそうなるのかわかりません。

いいえ。最初の例外は何の関係もありません-トレースバックでわかるように、 でr.info()例外が発生します。urllib2.urlopen(url)

BadStatusLine例外が定義されてhttplibおり、except urllib2.URLError単にそれをキャッチしません。おそらく、次のような例外処理ロジックを改善する必要があります。

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