Browse Source

trying to catch socket errors and restart on them

tags/v0.1
Ben Kurtovic 13 years ago
parent
commit
6d35a6964c
2 changed files with 19 additions and 5 deletions
  1. +10
    -5
      bot.py
  2. +9
    -0
      irc/actions.py

+ 10
- 5
bot.py View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-


## Imports ## Imports
import socket, string, re
import socket, string, re, time


from config.irc_config import * from config.irc_config import *
from config.secure_config import * from config.secure_config import *
@@ -14,11 +14,16 @@ def main():
read_buffer = str() read_buffer = str()


while 1: 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)) line = string.split(string.rstrip(line))
data = Data() data = Data()




+ 9
- 0
irc/actions.py View File

@@ -2,11 +2,20 @@


# Actions/commands to interface with IRC. # Actions/commands to interface with IRC.


import string

class Actions: class Actions:
def __init__(self, sock): def __init__(self, sock):
"""actions/commands to interface with IRC""" """actions/commands to interface with IRC"""
self.sock = sock 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): def send(self, msg):
"""send data to the server""" """send data to the server"""
self.sock.send(msg + "\r\n") self.sock.send(msg + "\r\n")


Loading…
Cancel
Save