[英]“Connection reset by peer” when using Twitch IRC in Python
I try to write my own bot in Python to connect and interact with the Twitch-chat via its IRC interface. 我尝试在Python中编写自己的bot,通过其IRC接口与Twitch-chat连接和交互。 I have the connection and can receive and send data.
我有连接,可以接收和发送数据。 I also answer the incoming PINGs with PONG (they get send after around 5 minutes).
我也用PONG回答传入的PING(他们在大约5分钟后发送)。 But after about 3 minutes of inactivity (so before I even get a PING) the connection fails with
但是在大约3分钟不活动之后(所以在我得到PING之前),连接失败了
socket.error: [Errno 104] Connection reset by peer
I use the socket
module in Python for the connection. 我使用Python中的
socket
模块进行连接。 The error occurs when the recv()
method is called on my socket. 在我的套接字上调用
recv()
方法时发生错误。
Some things I was able to figured out: 我能想到的一些事情:
Before the connection fails, their is no incoming (unanswered) PING. 在连接失败之前,它们没有传入(未应答)PING。
The time between the last chat message and the disconnect seams to be around 3 minutes every time. 上次聊天消息和断开连接之间的时间每次约为3分钟。 And its enough to receive something (linke a chat-message by someone else) to reset this timer.
它足以接收某些内容(由其他人发送聊天消息)来重置此计时器。 I don't have to send anything myself.
我不需要自己发送任何东西。
I first thought that I time out myself because I don't receive anything for to long. 我首先想到自己超时,因为我没有收到任何东西。 That doesn't seem to be the case, because if I set a lower timeout time for
socket.recv()
I receive socket.timeout: timed out
, not the error shown above. 这似乎并非如此,因为如果我为
socket.recv()
设置较低的超时时间,我会收到socket.recv()
socket.timeout: timed out
,而不是上面显示的错误。
I think I don't miss any data send to me because all chat messages come through. 我想我不会错过发送给我的任何数据,因为所有聊天消息都会通过。 But I post my main loop for receiving data below just to be sure.
但我发布我的主循环接收数据以确定。
My Question is now, why does the connection fail and how can I prevent this? 我的问题是,为什么连接失败,我该如何防止这种情况?
I have the feeling this is a vague question. 我觉得这是一个模糊的问题。 It's the first time I try to work with an IRC interface and I don't rely know how to get more information on what exactly the problem is.
这是我第一次尝试使用IRC界面而且我不知道如何获得有关问题究竟是什么的更多信息。
Just in case, here's the mains loop I use to receive data. 以防万一,这是我用来接收数据的电源回路。 I think this works fine because all the chat messages come through.
我认为这很好,因为所有的聊天消息都可以通过。 But maybe there's a mistake and I miss some incoming data (like a PING).
但也许有一个错误,我错过了一些传入的数据(如PING)。
readbuffer = ''
while True:
readbuffer = readbuffer + s.recv(1024)
temp = string.split(readbuffer, '\n')
readbuffer = temp.pop()
for line in temp:
print(line)
# PING/PONG
if "PING :tmi.twitch.tv" in line:
print("PONG :tmi.twitch.tv")
s.send(line.replace('PING', 'PONG'))
Here s
is a socket()
form the socket
module. 这里
s
是一个socket()
形成所述socket
模块。
Pinging the server myself every 2 minutes solved the problem. 每2分钟自己ping一次服务器就解决了这个问题。 I just added this function
我刚添加了这个功能
import time, threading
def sendPing(s):
print('sending PING')
s.send("PING :tmi.twitch.tv")
threading.Timer(120, sendPing, [s]).start()
sendPing(s)
before the main loop. 在主循环之前。
Thanks to ChatterOne for the tip. 感谢ChatterOne的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.