Python IRC Bot 静默超时(?)- 突然停止工作

Python IRC Bot silent timeout(?) - Stops working suddenly

我最近开始编写自己的 IRC Bot,没什么特别的,只是读取频道并发布特定命令的文本文件内容。

# let's connect
irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc_socket.connect((server, 6667))

# now we login and send a test message
irc_socket.send(str.encode('PASS ' + pw + '\r\n'))
irc_socket.send(str.encode('NICK ' + bot + '\r\n'))
irc_socket.send(str.encode('USER ' + bot + '\r\n'))
irc_socket.send(str.encode('JOIN #' + channel + '\r\n'))

while 1:
    text = irc_socket.recv(2040).decode()
    if text.startswith('!ideen'):
        do stuff #lot of code, shouldn't be of importance

奇怪的是,一切都按照我的意愿运行,但由于某些原因,它在一段时间后停止运行。它不会超时或给出任何错误,它只是完全停止反应。首先,我认为它会因消息过多而过载,但如果频道只是空着 20 分钟左右,也会发生这种情况。当我从 Pycharm 或命令行 运行 时发生同样的情况(不确定为什么会有所作为)。

我可能不是唯一遇到问题的人,但我找不到任何东西。感谢您的帮助,甚至是有关如何调试此类内容的提示。

IRC 频道是一个 Twitch-IRC 聊天频道(如果它很重要的话)。

编辑:尝试了@matt-m 调试提示,他似乎停止接收消息。文本消息始终是空字符串。

您是否可能没有使用 PONG 响应服务器 PING

https://www.rfc-editor.org/rfc/rfc2812#section-3.7.2

If a connection fails to respond to a PING message within a set amount of time, that connection is closed.

所以你应该确保你这样做了。我认为它会在连接关闭时引发一些套接字错误,但根据 How to tell if a connection is dead in python,情况可能并非总是如此。