Python BOT keeps disconnecting, even with PONGs

Hello,

I have an issue with my Python bot, I can make it connect and have it made to send PONGs to the server, but even then it keeps disconnecting and suddenly I can’t receive messages from the server.
This seems to be especially prominent if the chat is inactive for a while, if people keep chatting it doesn’t seem to be an issue. On at least one occasion it has happened that it even disconnected before the first ping.

Here is the least amount of code I can use to recreate the problem:

import socket

HOST = ‘irc.chat.twitch.tv.’
PORT = 6667
PASS = “XXXXX”
NICK = “testbotroro”
CHANNEL = “imroroyo”

def main():
s = socket.socket()
s.connect((HOST, PORT))
s.send(“PASS {}\r\n”.format(PASS).encode(“utf-8”))
s.send(“NICK {}\r\n”.format(NICK).encode(“utf-8”))
s.send(“JOIN #{}\r\n”.format(CHANNEL).encode(“utf-8”))

while True:
    response = s.recv(1024).decode("utf-8")
    print(response)
    if response == "PING :tmi.twitch.tv\r\n":
        s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
        print("---SENT PONG---")

if name == “main”:
main()

1 Like

irc.chat.twitch.tv. I would suggest to remove the trailing ., but aside from that and the indenting I don’t see any errors. It could be that s.recv(1024) doesn’t receive the entire message.
Here is an IRC wrapper I made, if you’re interested in how I handle everything.

I mean I can connect to the server and retrieve messages, make them print in the console. But the issue is that the connection times out after a while. You sure these issues you are talking about could be connected to that?

The only reason I know for getting disconnected is not sending PONG correctly, but I don’t think I can find any problems with your code.

That’s so strange, have been thinking about if the bot just stops doing the s.recv(1024) function for some reason? But I don’t see why it would do that?

Try putting it into a buffer, and only continue when you are certain it has a full message.

I used to have this:

readbuffer = “”

while True:
readbuffer = readbuffer + s.recv(1024).decode(“utf-8”)
temp = str.split(readbuffer, “\n”)
readbuffer = temp.pop()

to generate the server message, but this had the same issue as I’m having right now.

That’s because this will still have the same issue as earlier. try using

buf = ""
while True:
    buf = buf + s.recv(1024).decode()
    if '\r\n' in buf:
        tmp = buf.split('\r\n')
        msg = tmp.pop(0)
        buf = '\r\n'.join(tmp)
        # parse message further

It’s getting late for me so this is kinda ugly, but I think it works.

Hey, sorry it took me a while to respond.

I am trying out this code now then: http://pastebin.com/4yZr6m3C

Do you think that should work? I will come back and say how it’s going!

Okey so I tried out the code in my previous reply.

Firstly the console didn’t print properly and was basically delayed by about 5-6 messages. So when I typed something in the IRC chat, another old line would be printed in the console (although delayed of course).

Also the bot disconnected once again, sadly.

Hey, this is my code for dealing with data; (I’m using Python 2.7.11)

Attempts a data grab from twitch

self.data = sock.recv(1024)
self.databuffer = self.databuffer + self.data #Appends data received to buffer
self.temp = self.databuffer.rsplit(’\n’) #Splits buffer into list
self.databuffer = self.temp.pop() #Grabs the last list item and assigns to buffer

This works really well with minimal latency (ignore the “self.”)

For the ping response I see an issue:

PONG :tmi.twitch.tv\r\n

Which should be:

PONG tmi.twitch.tv\r\n

Hopefully this works for you.

In the future, I recommend grabbing the ping string sent by twitch, remove the colon and send it back, this makes sure the ping response won’t break in the event that twitch decides to change the address.

You only need to send back PONG\r\n and it’ll work. There’s no need for the address after it.

the part that’s bugging me is recv(1024) since it blocks UNTIL it receives 1024 bytes. Now what if it receives less?
It will wait, and not respond to the PING that might’ve happened. This is why I’m shocked by the fact that the socket library doesn’t provide a readline method.

According to recv documentation, it receives a maximum of 1024 (or whatever is specified) bytes, not exactly that.

Last time I used it it did :thinking: must’ve updated I guess

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.