使用itchat过程碰到urllib3的异常

在用itchat做微信防撤回的时候,出现了这么个异常,而且没有找到对应的代码,好像是urllib3的库的原因,不能处理0这个HTTP响应码。难道我要去改urllib3?

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    six.raise_from(e, None)
  File "", line 2, in raise_from
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 285, in _read_status
    raise BadStatusLine(line)
http.client.BadStatusLine: HTTP/1.1 0 -


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 649, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    six.raise_from(e, None)
  File "", line 2, in raise_from
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 285, in _read_status
    raise BadStatusLine(line)
urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine('HTTP/1.1 0 -\r\n',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/itchat/components/login.py", line 231, in maintain_loop
    i = sync_check(self)
  File "/usr/local/lib/python3.6/site-packages/itchat/components/login.py", line 283, in sync_check
    r = self.s.get(url, params=params, headers=headers)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 521, in get
    return self.request('GET', url, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 490, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('HTTP/1.1 0 -\r\n',))

然后找到这个
https://stackoverflow.com/questions/27619258/httplib-badstatusline
说是捕获一下异常,然后pass,但是我连这个异常的代码在哪我都不知道。
然后看到之前itchat的issue里提的问题,也出现过这种情况。
https://github.com/littlecodersh/ItChat/issues/353
https://github.com/littlecodersh/ItChat/issues/424
但是貌似都没有解决。
那就老老实实看看代码吧。根据异常栈找到raise出这个异常的地方:在我电脑上是/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py

 257     def _read_status(self):
 258         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
 259         if len(line) > _MAXLINE:
 260             raise LineTooLong("status line")
 261         if self.debuglevel > 0:
 262             print("reply:", repr(line))
 263         if not line:
 264             # Presumably, the server closed the connection before
 265             # sending a valid response.
 266             raise RemoteDisconnected("Remote end closed connection without"
 267                                      " response")
 268         try:
 269             version, status, reason = line.split(None, 2)
 270         except ValueError:
 271             try:
 272                 version, status = line.split(None, 1)
 273                 reason = ""
 274             except ValueError:
 275                 # empty version will cause next test to fail.
 276                 version = ""
 277         if not version.startswith("HTTP/"):
 278             self._close_conn()
 279             raise BadStatusLine(line)
 280
 281         # The status code is a three-digit number
 282         try:
 283             status = int(status)
 284             if status < 100 or status > 999:
 285                 raise BadStatusLine(line)
 286         except ValueError:
 287             raise BadStatusLine(line)
 288         return version, status, reason

从#284可以看出,状态码必须是[100,999]之间的三位数,而我们这里的状态码是0,所以抛出了异常。
//TODO

你可能感兴趣的:(网络编程)