created python twitch chat bot, keeps timing out and disconnecting. any ideas why? reddit / irc ---------------------------------------------------------------------- Phildos · 2017-02-01 19:28 Here's the simple twitch module: import string import socket import time from collections import deque from config import HOST, PORT, PASS, IDENT, CHANNEL class Twitch(): def __init__(self): pass def sendMessage(self, message): messageTemp = "PRIVMSG #" + CHANNEL + " :" + message self.s.send(messageTemp + "\r\n") def getMessage(self, line): separate = line.split(":", 2) message = separate[2] return message def initialize(self): # opens socket self.s = socket.socket() self.s.connect((HOST, PORT)) self.s.send("PASS " + PASS + "\r\n") self.s.send("NICK " + IDENT + "\r\n") self.s.send("JOIN #" + CHANNEL + "\r\n") # joins room self.readbuffer = "" Loading = True while Loading: self.readbuffer = self.readbuffer + self.s.recv(1024) temp = string.split(self.readbuffer, "\n") self.readbuffer = temp.pop() for line in temp: Loading = not ("End of /NAMES list" in line) # initialize properties self.readbuffer = "" self.q = deque([]) def tick(self): self.readbuffer = self.readbuffer + self.s.recv(1024) temp = string.split(self.readbuffer, "\n") self.readbuffer = temp.pop() for line in temp: if "PING" in line: self.s.send(line.replace("PING", "PONG")) else: self.q.appendleft(self.getMessage(line)) def next(self): if len(self.q) > 0: return self.q.pop() else: return None My code is a simplified version of this: https://github.com/BadNidalee/ChatBot and the main loop that runs it is just this: from twitch import Twitch import re import time t = Twitch() t.initialize() while True: try: t.tick() line = t.next() val = 0.0 while line is not None: words = line.split() if words[0] == "hello": t.sendMessage("hi there") line = t.next() except: print "gave up!" time.sleep(1) break After around 10-20 minutes, it just hangs. Is there something about the IRC protocol I'm not getting? Or is there something regarding python sockets I'm doing wrong? Any help would be great! ---------------------------------------------------------------------- jhoneycutt · 2017-02-01 21:09 if "PING" in line: self.s.send(line.replace("PING", "PONG")) I think this is your issue. Your PONGs are never processed by the server because you forgot to terminate this line with a "\r\n". You should add a "sendToServer" method that always appends a "\r\n" so you can't make this mistake in the future (and so you don't have to clutter your code with "\r\n" for every line you send). ---------------------------------------------------------------------- Phildos · 2017-02-01 23:11 aha! ok I'll try that. But, since "temp" is split on "\n", doesn't that mean the line will still have the "\r"? so all I should need to add back is the "\n"- right? (Trying now, will get back with results. Just need to wait and am thinking out loud hah. Thanks!) ---------------------------------------------------------------------- Phildos · 2017-02-02 02:38 alright! appended the "\n" and it seems to work! thanks! :)