From 6d35a6964cd4e04112b62a4b4e90d4076b094e1d Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 14 Apr 2011 17:14:47 -0400 Subject: [PATCH] trying to catch socket errors and restart on them --- bot.py | 15 ++++++++++----- irc/actions.py | 9 +++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bot.py b/bot.py index 12e6a13..539c33b 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- ## Imports -import socket, string, re +import socket, string, re, time from config.irc_config import * from config.secure_config import * @@ -14,11 +14,16 @@ def main(): read_buffer = str() while 1: - read_buffer = read_buffer + actions.sock.recv(1024) - temp = string.split(read_buffer, "\n") - read_buffer = temp.pop() + try: + read_buffer = read_buffer + actions.get() + except RuntimeError: # socket broke + time.sleep(60) # sleep for sixty seconds + return # then exit our loop and restart the bot - for line in temp: + lines = string.split(read_buffer, "\n") + read_buffer = lines.pop() + + for line in lines: line = string.split(string.rstrip(line)) data = Data() diff --git a/irc/actions.py b/irc/actions.py index 2b3bddf..4bbf1d6 100644 --- a/irc/actions.py +++ b/irc/actions.py @@ -2,11 +2,20 @@ # Actions/commands to interface with IRC. +import string + class Actions: def __init__(self, sock): """actions/commands to interface with IRC""" self.sock = sock + def get(self, size = 4096): + """receive (get) data from the server""" + data = self.sock.recv(4096) + if not data: + raise RuntimeError("socket is dead") + return data + def send(self, msg): """send data to the server""" self.sock.send(msg + "\r\n")