Nereos
Nereos

Reputation: 111

"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. 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). But after about 3 minutes of inactivity (so before I even get a PING) the connection fails with

socket.error: [Errno 104] Connection reset by peer

I use the socket module in Python for the connection. The error occurs when the recv() method is called on my socket.


Some things I was able to figured out:


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.


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).

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.

Upvotes: 1

Views: 1337

Answers (1)

Nereos
Nereos

Reputation: 111

Pinging the server myself every 2 minutes solved the problem. 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.

Upvotes: 3

Related Questions