Python中的IRC连接问题
我有一个Python脚本,它有一个无限的while循环。它加入一个IRC通道,进行一些计算,打印一些消息,休眠5分钟,然后重复。脚本会定期出现以下错误:Python中的IRC连接问题,python,irc,Python,Irc,我有一个Python脚本,它有一个无限的while循环。它加入一个IRC通道,进行一些计算,打印一些消息,休眠5分钟,然后重复。脚本会定期出现以下错误: 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 当连接关闭(EO
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
当连接关闭(EOF)时,ircsock.recv(4096)
返回一个空字符串。反过来,'.split()
返回空列表。并尝试从空列表中获取第一项[[0]
将引发索引器
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' )