2014-03-28 4 views
0

나는 while 루프가 무한대 인 Python 스크립트를 가지고 있습니다. IRC 채널에 참여하고, 계산을하고, 메시지를 출력하고, 5 분 동안 잠자기 한 다음 반복합니다. 주기적으로 스크립트 오류 밖으로 다음Python의 IRC 연결 문제

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 

인터넷 채팅 핸들은 다음과 같은 메시지 채널 잎 다음과 같이 (03:00:56 PM) TestBOT left the room (quit: Ping timeout: 121 seconds).

내 코드는 다음과 같습니다

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') 

답변

1

연결이 종료 (EOF)이면 ircsock.recv(4096)은 빈 문자열을 반환합니다. 차례로 ''.split()은 빈 목록을 반환합니다. 그리고 빈 목록에서 첫 번째 항목을 얻으려고하면 [][0]가 발생합니다. IndexError.