|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
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
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev Last edited by xlordt : November 17th, 2003 at 03:33 AM. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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," ") Quote:
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 09:35 AM. |
|
#4
|
||||
|
||||
|
Quote:
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. |
|
#5
|
||||
|
||||
|
Quote:
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 ![]() |
|
#6
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > IndexError: |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|