python 电报机器人轮询()套接字超时

nc1teljy  于 2023-01-08  发布在  Python
关注(0)|答案(6)|浏览(165)

当我让我的程序运行几个小时,然后发送消息给机器人它不回复,过了一段时间,它生成以下错误。

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1322, in getresponse
    response.begin()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 303, in begin
    version, status, reason = self._read_status()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 264, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
    return self._sock.recv_into(b)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ssl.py", line 1099, in read
    return 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 "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/util/retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 447, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/urllib3/connectionpool.py", line 336, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=30)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "botTest.py", line 28, in <module>
    bot.polling()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/__init__.py", line 617, in polling
    self.__threaded_polling(none_stop, interval, timeout, long_polling_timeout, allowed_updates)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/__init__.py", line 676, in __threaded_polling
    raise e
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/__init__.py", line 639, in __threaded_polling
    self.worker_pool.raise_exceptions()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/util.py", line 130, in raise_exceptions
    raise self.exception_info
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/util.py", line 82, in run
    task(*args, **kwargs)
  File "botTest.py", line 25, in echo_message
    bot.reply_to(message, message.text)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/__init__.py", line 2147, in reply_to
    return self.send_message(message.chat.id, text, reply_to_message_id=message.message_id, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/__init__.py", line 919, in send_message
    apihelper.send_message(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/apihelper.py", line 257, in send_message
    return _make_request(token, method_url, params=payload, method='post')
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/telebot/apihelper.py", line 139, in _make_request
    result = _get_req_session().request(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.telegram.org', port=443): Read timed out. (read timeout=30)

据我所知,问题出在bot.polling()发出的请求上。
我也尝试了示例文件echo_bot.py,但问题是相同的。我尝试了不同的解决方案:

bot.polling(none_stop=True, timeout=30)

以及

try:
        bot.polling(none_stop=True)

    except Exception as e:
        time.sleep(15)

不幸的是,都有同样的问题。一开始它工作正常,几个小时后就停止响应了。
这是示例代码

import telebot

API_TOKEN = 'API_TOKEN'

bot = telebot.TeleBot(API_TOKEN)

@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    bot.reply_to(message, "Hi there, I am EchoBot.")

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    bot.reply_to(message, message.text)

bot.polling()

谢谢大家的帮助

efzxgjgh

efzxgjgh1#

最后,我通过将bot.polling()替换为以下内容解决了这个问题:

bot.infinity_polling(timeout=10, long_polling_timeout = 5)

但是,这可能会导致(不是每个人)丢失消息(显然只在开始时,几天后问题似乎自行消失)。

如您所见,我已发送了5条消息,但只有3条回复

我会尽快更新这个答案,但问题解释如下:
https://github.com/eternnoir/pyTelegramBotAPI/issues/1259

yv5phkfx

yv5phkfx2#

import os, sys
from requests.exceptions import ConnectionError, ReadTimeout

try:
    bot.infinity_polling(timeout=10, long_polling_timeout=5)
except (ConnectionError, ReadTimeout) as e:
    sys.stdout.flush()
    os.execv(sys.argv[0], sys.argv)
else:
    bot.infinity_polling(timeout=10, long_polling_timeout=5)

这对我很有效,我只是重新启动脚本,哈哈

bnl4lu3b

bnl4lu3b3#

试试

bot.infinity_polling(True)
j9per5c4

j9per5c44#

我也遇到过这个问题,有些人使用web hook方法,实际上这是一个最好的和适当的解决方案。无论如何,在我的情况下,我在学习web hook之前尝试了这个想法,它帮助我保持机器人运行,并在每次断开连接时继续尝试重新连接。这很容易和有趣)

import telebot,time

if __name__=='__main__':
while True:
    try:
        bot.polling(non_stop=True, interval=0)
    except Exception as e:
        print(e)
        time.sleep(5)
        continue
whitzsjs

whitzsjs5#

这个选项帮助我

while True:
try:
    asyncio.run(bot.polling(non_stop=True, interval=1, timeout=0))
except:
    time.sleep(5)
jgzswidk

jgzswidk6#

将超时增加到90并添加try:

while True:
    try:
        bot.polling(none_stop=True, timeout=90)
    except Exception as e:
        print(datetime.datetime.now(), e)
        time.sleep(5)
        continue

相关问题