# Python BOT keeps disconnecting, even with PONGs

**URL:** <https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573>\
**Category:** Chat and IRC\
**Created:** [September 24, 2016, 1:13pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573 "2016-09-24T13:13:28Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![imroroyo](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@imroroyo](https://discuss.dev.twitch.com/u/imroroyo)\
**Post date:** [September 24, 2016, 1:13pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/1 "2016-09-24T13:13:28Z")

</div>

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](http://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()

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [September 24, 2016, 6:39pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/2 "2016-09-24T18:39:04Z")

</div>

`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](http://github.com/martmists/asynctwitch) is an IRC wrapper I made, if you’re interested in how I handle everything.

---

<div class="post-metadata">

**Author:** ![imroroyo](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@imroroyo](https://discuss.dev.twitch.com/u/imroroyo)\
**Post date:** [September 24, 2016, 6:46pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/3 "2016-09-24T18:46:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [September 24, 2016, 6:48pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/4 "2016-09-24T18:48:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![imroroyo](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@imroroyo](https://discuss.dev.twitch.com/u/imroroyo)\
**Post date:** [September 24, 2016, 7:03pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/5 "2016-09-24T19:03:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [September 24, 2016, 7:07pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/6 "2016-09-24T19:07:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![imroroyo](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@imroroyo](https://discuss.dev.twitch.com/u/imroroyo)\
**Post date:** [September 24, 2016, 7:10pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/7 "2016-09-24T19:10:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [September 24, 2016, 7:16pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/8 "2016-09-24T19:16:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![imroroyo](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@imroroyo](https://discuss.dev.twitch.com/u/imroroyo)\
**Post date:** [September 25, 2016, 9:43am UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/9 "2016-09-25T09:43:21Z")

</div>

Hey, sorry it took me a while to respond.

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

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

---

<div class="post-metadata">

**Author:** ![imroroyo](https://avatars.discourse-cdn.com/v4/letter/i/e9bcb4/32.png) [@imroroyo](https://discuss.dev.twitch.com/u/imroroyo)\
**Post date:** [September 25, 2016, 10:03am UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/10 "2016-09-25T10:03:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TheKillar25](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/thekillar25/32/2878_2.png) [@TheKillar25](https://discuss.dev.twitch.com/u/TheKillar25)\
**Post date:** [October 1, 2016, 7:15pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/11 "2016-10-01T19:15:29Z")

</div>

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](http://tmi.twitch.tv)\r\n

Hopefully this works for you.

---

<div class="post-metadata">

**Author:** ![TheKillar25](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/thekillar25/32/2878_2.png) [@TheKillar25](https://discuss.dev.twitch.com/u/TheKillar25)\
**Post date:** [October 1, 2016, 7:17pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/12 "2016-10-01T19:17:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [October 1, 2016, 7:30pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/13 "2016-10-01T19:30:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [October 4, 2016, 5:05pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/14 "2016-10-04T17:05:16Z")

</div>

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`](https://docs.python.org/3/library/socket.html) library doesn’t provide a `readline` method.

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [October 4, 2016, 5:17pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/15 "2016-10-04T17:17:37Z")

</div>

According to [recv documentation](https://docs.python.org/3/library/socket.html#socket.socket.recv), it receives a _maximum_ of 1024 (or whatever is specified) bytes, not exactly that.

---

<div class="post-metadata">

**Author:** ![martmists](https://avatars.discourse-cdn.com/v4/letter/m/48db29/32.png) [@martmists](https://discuss.dev.twitch.com/u/martmists)\
**Post date:** [October 4, 2016, 5:18pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/16 "2016-10-04T17:18:40Z")

</div>

Last time I used it it did 🤔 must’ve updated I guess

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [November 3, 2016, 5:18pm UTC](https://discuss.dev.twitch.com/t/python-bot-keeps-disconnecting-even-with-pongs/7573/17 "2016-11-03T17:18:54Z")

</div>

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