Reputation: 43
I have a Python script which has an infinite while loop. It joins an IRC channel, does some computation, prints some message, sleeps for 5 minutes, and then repeats. Periodically the scripts errors out with the following:
Traceback (most recent call last):
File "db_alerts.py", line 65, in <module>
if data.split()[0].find('PING') != -1:
IndexError: list index out of range
The IRC handle leaves the channel with the following message: (03:00:56 PM) TestBOT left the room (quit: Ping timeout: 121 seconds).
My code is as follows:
import json
import socket
import time
if status:
env = "prod"
time_of_last_check = 0
channelfile = 'irc_channels.json' #File to store channel and password in json format
channelinfo = open(channelfile,'r').read()
botnick = json.loads(channelinfo)[env]["nick"]
network = 'irc.xxxx.com'
ircsock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
ircsock.connect((network, 6667))
ircsock.send("NICK "+ botnick +"\r\n")
ircsock.send('USER '+ botnick +' userrxxxxx - :'+botnick +"\r\n")
time.sleep(2)
ircsock.send ( 'PRIVMSG NickServ :IDENTIFY zzzzzzzz\r\n')
for channel,password in json.loads(channelinfo)[env]["channel"].iteritems():
ircsock.send("JOIN "+ channel + " " + password + "\r\n")
while 1:
if time.time() - time_of_last_check > 350:
localtime = time.asctime( time.localtime(time.time()) )
print "Last run time :", localtime
for message,ticket in get_data():
if str(message) != 'None' and str(ticket) != 'None':
for channel,password in json.loads(channelinfo)[env]["channel"].iteritems():
if channel == "#X":
message = "TEST"
ircsock.send('PRIVMSG %s :%s \r\n' % (channel,message))
time.sleep(1)
else:
message1 = "TEST1"
ircsock.send('PRIVMSG %s :%s \r\n' % (channel,message1))
time.sleep(1)
data = ircsock.recv (4096)
if data.split()[0].find('PING') != -1:
ircsock.send('PONG ' + data.split()[1] + '\r\n' )
for message,ticket in get_data1():
if str(message) != 'None' and str(ticket) != 'None':
for channel,password in json.loads(channelinfo)[env]["channel"].iteritems():
if channel == "#X":
message = "TEST"
ircsock.send('PRIVMSG %s :%s \r\n' % (channel,message))
time.sleep(1)
else:
message1 = "TEST1"
ircsock.send('PRIVMSG %s :%s \r\n' % (channel,message1))
time.sleep(1)
time_of_last_check = time.time()
data = ircsock.recv (4096)
if data.split()[0].find('PING') != -1:
ircsock.send('PONG ' + data.split()[1] + '\r\n' )
Upvotes: 0
Views: 175
Reputation: 414215
When connection is closed (EOF), ircsock.recv(4096)
returns an empty string. In turn ''.split()
returns empty list. And trying to get the first item from an empty list [][0]
raises IndexError
.
Upvotes: 1