Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 17th, 2003, 03:30 AM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
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 03:33 AM.

Reply With Quote
  #2  
Old November 17th, 2003, 09:02 AM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
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.

Reply With Quote
  #3  
Old November 17th, 2003, 09:32 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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:
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : November 17th, 2003 at 09:35 AM.

Reply With Quote
  #4  
Old November 17th, 2003, 10:27 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
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.

Reply With Quote
  #5  
Old November 17th, 2003, 04:16 PM
xlordt's Avatar
xlordt xlordt is offline
Only the strong survives!!.
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Feb 2003
Location: A World of wonders.
Posts: 5,573 xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)xlordt User rank is Major (30000 - 40000 Reputation Level)  Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1Folding Points: 111202 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 11 h 9 m 33 sec
Reputation Power: 406
Send a message via ICQ to xlordt Send a message via AIM to xlordt Send a message via MSN to xlordt Send a message via Yahoo to xlordt Send a message via Google Talk to xlordt Send a message via Skype to xlordt
Facebook MySpace
Quote:
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

Reply With Quote
  #6  
Old November 17th, 2003, 07:07 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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
File Type: py pyrc.py (921 Bytes, 417 views)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > IndexError:

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap