youfou / wxpy

微信机器人 / 可能是最优雅的微信个人号 API ✨✨
http://wxpy.readthedocs.io
MIT License
13.99k stars 2.38k forks source link

读取消息超时后退出 #206

Open lichongsw opened 7 years ago

lichongsw commented 7 years ago

环境如下: ubuntu 16.04, python 3.5.2 requests 2.18.4 urllib3 1.22

移动端ipad挂上微信直接充电不锁屏。网页版接上图灵机器人,之后每2分钟发一条不重复消息给机器人。大概24小时之后网页版退出,日志如下

Traceback (most recent call last):
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 387, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 383, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.5/http/client.py", line 1197, in getresponse
    response.begin()
  File "/usr/lib/python3.5/http/client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
  File "/usr/lib/python3.5/ssl.py", line 929, in recv_into
    return self.read(nbytes, buffer)
  File "/usr/lib/python3.5/ssl.py", line 791, in read
    return self._sslobj.read(len, buffer)
  File "/usr/lib/python3.5/ssl.py", line 575, in read
    v = self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lc/.local/lib/python3.5/site-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/util/retry.py", line 357, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/packages/six.py", line 686, in reraise
    raise value
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 389, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/home/lc/.local/lib/python3.5/site-packages/urllib3/connectionpool.py", line 309, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='wx2.qq.com', port=443): Read timed out. (read timeout=20)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lc/.local/lib/python3.5/site-packages/itchat/components/login.py", line 237, in maintain_loop
    msgList, contactList = self.get_msg()
  File "/home/lc/.local/lib/python3.5/site-packages/itchat/components/login.py", line 302, in get_msg
    r = self.s.post(url, data=json.dumps(data), headers=headers)
  File "/home/lc/.local/lib/python3.5/site-packages/requests/sessions.py", line 555, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/home/lc/gits/wxpy/wxpy/utils/misc.py", line 351, in customized_request
    return requests.Session.request(session, method, url, **kwargs)
  File "/home/lc/.local/lib/python3.5/site-packages/requests/sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/lc/.local/lib/python3.5/site-packages/requests/sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "/home/lc/.local/lib/python3.5/site-packages/requests/adapters.py", line 521, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='wx2.qq.com', port=443): Read timed out. (read timeout=20)

LOG OUT!

这里没有微信后台关闭的日志(RemoteDisconnected) https://github.com/littlecodersh/ItChat/issues/361

也没有网络问题(BadStatusLine)

115

查看之前类似的issue,有网络超时警告,不影响运行,不确认会有其他影响。目前这里是是直接退出了,是不是有新的异常没有处理? @zzir 你还有可以在线超过二十几个小时的环境吗?能够帮忙看看你的运行环境吗?感激不尽

103

@youfou 会不会像这位仁兄碰到的问题一样?(虽然没有讲清楚,但我也打算用requests 2.18.3来再尝试一下,有新的结果再来补充) https://github.com/littlecodersh/ItChat/issues/512

zzir commented 7 years ago

我之前遇到的报错和你的一样,不过我的程序还是能正常运行的,最长我试过一周没有登出。

你是否开启了回话缓存呢? bot = Bot(cache_path=True)

或者登出回调。

https://github.com/youfou/wxpy/blob/master/wxpy/api/bot.py#L54

lichongsw commented 7 years ago

@zzir 谢谢你的回复。缓存用了,登出回调没有使用。

在换用requests 2.18.3之后现在已经在线2天了,暂时没有再报类似的问题。