Thread: IndexError:

    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    IndexError:


    Ok i created an irc bot using the socket module, but.. its bugged.. when i am in irc.. and i kill the bot, in my konsole i get this error

    Traceback (most recent call last):
    File "./4.py", line 39, in ?
    print Recv(s,1025)
    File "./4.py", line 16, in Recv
    if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot":
    IndexError: list index out of range

    Can anyone tell me what this means?
    all so when the bot enters the chan i made it say hello, but when i view my konsole i dont see anything about the bot saing hello all i see is what the users in the irc inputed.. here is the code

    Code:
    #!/usr/bin/env python
    
    from socket import *
    from string import *
    
    
    def Send(self,data):
     self.send(data)
     
    def Recv(self,integras):
     data = self.recv(integras) 
     receive = split(data," ")
     
     if receive[0] == "PING": Send(self,"PONG :"+receive[0]+"\n\r")
     
     if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot": 
      user = split(receive[0],"!") 
      Send(self,"PRIVMSG #bot :Dont bother me im just a stupid bot\n\r")
      return "PRV From"+user[0]+"\\"+receive[3]
      
     #if receive[3] == "!quit": Send(s,"QUIT\n\r")
     
     else: 
      return data
     
    s = socket(AF_INET,SOCK_STREAM) #Initializing TCP
    msg = 0
    
    try:
     if s.connect(("irc.server.net",6667)) != -1:
     
      Send(s,"NICK Pythonbot\n\r")
      Send(s,"USER localhost localhost localhost : ****YOU\n\r")
      Send(s,"JOIN #bot\n\r")
      msg = "hello"
      Send(s,"PRIVMSG #bot :"+msg+"\n\r")
      
      while 1: 
       print Recv(s,1025)
       msg =+ 1
        
    except error, detail:
     print detail[0]
    sorry still new to python, and still learning
    any suggestions, Since python are not to big on tutorials
    Last edited by xlordt; November 17th, 2003 at 04:33 AM.
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12
    I guess this could be your mistake :
    Code:
    Send(s,"NICK Pythonbot\n\r")
    You're sending a string to your script with one whitespace.
    Code:
    receive = split(data," ")
    This creates a tuple with 2 elements. So here comes the problem:
    Code:
    if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot":
    I'd say that's the problem. You're trying to access the third element of your 2-tuple ! Remember, Python's lists and tuples work like C arrays : the nth element is accessed by number n-1. So you only need to change it that way :
    Code:
    if receive[0] == "PRIVMSG" and receive[1] == "Pythonbot":
    This should do the trick.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Wow, very surprised! i figured that an IRC bot would be allot bigger than this, my bad

    Anyway looks like you beat me too it solar .. anyway i think your right, obviously your trying to access values which dont exist X , too be safe you could check its size before you carry on although you dont 'need' too..

    Code:
    if len(recieve) > 1:
        ...
        carry on
        ...
    Next , you really don't need the string module anymore (at least in my oppinion), you can do everything the string module does more efficently using string methods i.e.

    Code:
    receive = data.split()
    If you insist on using the string module though you should know that split() defaults to spaces so you should write..

    Code:
    receive = split(data)
    ..instead of..

    Code:
    receive = split(data," ")
    Solar
    This creates a tuple with 2 elements. So here comes the problem:
    Its actually a list not a tuple. it may not seem like a big deal but there are allot of things you just can't do with tuples so i figured i'd just point this one out

    Mark.
    Last edited by netytan; November 17th, 2003 at 10:35 AM.
    programming language development: www.netytan.com Hula

  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    def Send(self, data):
    ...
    ...

    def Recv(self, integras):
    ...
    ...
    I just knowticed the 'self' option in your function definitions.. really not the best idea X its would probably help to think of 'self' as a reserved word (although it isnt actually) for use inside classes.. possibly use 'this' or 'object' etc.

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    This creates a tuple with 2 elements. So here comes the problem:

    code:if receive[1] == "PRIVMSG" and receive[2] == "Pythonbot":
    are you sure.. cause.. see what im doing is im splitting the data that i get before it enters the irc chan, there for when i split it then receive[1] will be == to PRIVMSG, now receive[0] will be the users host... im confused now
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok for everyone else not part of the Netytan -> X meeting, we've solved the problem and X came up with an even smaller piece of code than the one he had before (this one works)

    Anyway i created a class to handle this (attached), feel free to download and learn..

    Mark.
    Attached Files
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo