使用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 urlopenchunked=chunked)File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 386, in _make_requestsix.raise_from(e, None)File "", line 2, in raise_fromFile "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 382, in _make_requesthttplib_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 getresponseresponse.begin()File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in beginversion, 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_statusraise 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 sendtimeout=timeoutFile "/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 incrementraise six.reraise(type(error), error, _stacktrace)File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraiseraise value.with_traceback(tb)File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopenchunked=chunked)File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 386, in _make_requestsix.raise_from(e, None)File "", line 2, in raise_fromFile "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 382, in _make_requesthttplib_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 getresponseresponse.begin()File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/http/client.py", line 297, in beginversion, 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_statusraise 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_loopi = sync_check(self)File "/usr/local/lib/python3.6/site-packages/itchat/components/login.py", line 283, in sync_checkr = self.s.get(url, params=params, headers=headers)File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 521, in getreturn self.request('GET', url, **kwargs)File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 508, in requestresp = self.send(prep, **send_kwargs)File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 618, in sendr = adapter.send(request, **kwargs)File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 490, in sendraise 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 before265             # 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)280281         # The status code is a three-digit number282         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


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部