首页 > 解决方案 > 为什么远程机器人中的 ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer')) 会发生?

问题描述

我写了电报机器人并在美国地区的谷歌引擎上运行它(电报在我的国家被封锁)。大多数时候,一切正常,但有时机器人开始抛出错误

('连接中止。',ConnectionResetError(104,'连接被对等方重置'))

他两次给出这个错误(在两条消息上),第三次他回答得很好。在互联网上,这个问题的答案通常是代理问题。我不使用代理。有没有人遇到过类似的问题?有什么方法可以解决吗?

代码非常简单:

@bot.message_handler(commands=['check'])
def send_text(message):
    try:
        # Here bot connects to my db and gets info from it
        bot.send_message(message.chat.id, 'some message')
    except Exception as e:
        print(e)
        print("Problem in check function: ", message.chat.id)

if __name__ == '__main__':
    while True:
        try:
            bot.polling(none_stop=True, timeout=30)
        except:
            time.sleep(15)

标签: proxybotstelegramtelegram-botpython-telegram-bot

解决方案


由于与服务器的连接打开时间过长,因此引发了异常,因此它被拒绝。在您的机器人初始化中设置此参数以每 5 分钟重置一次连接:

apihelper.SESSION_TIME_TO_LIVE = 60 * 5

推荐阅读