Socket problems

J

jjbutler88

I am trying to write a simple python IRC client, roughly following
this guide: http://www.devshed.com/c/a/Python/Python-and-IRC/

I have written some code, which uses the same commands as the guide,
but I get this error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/
python2.5/threading.py", line 486, in __bootstrap_inner
self.run()
File "pythonirc.py", line 31, in run
self.sock.send('NICK %s\r\n') % self.nick
AttributeError: send

Here is my code so far:
Code:
#!/usr/bin/env python

from socket import *
from threading import Thread
import sys

class IRCBot(Thread):
    def __init__(self, host, room, nick, port=6667, ssl=0):
        Thread.__init__(self)
        self.host = host
        self.port = port
        self.ssl = ssl
        self.room = room
        self.nick = nick
        self.sock = socket(AF_INET, SOCK_STREAM)

    def run(self):
        print "Connecting..."
        try:
            self.sock.connect((self.host, self.port))
        except:
            print "Could not connect to %s" % self.host
            sys.exit(1)
        if self.ssl:
            try:
                self.sock = ssl(self.sock)
            except:
                print "Server does not suport SSL"
                sys.exit(1)

        self.sock.send('NICK %s\r\n') % self.nick
        self.sock.send('USER PyIRC PyIRC PyIRC :Python IRC\r\n')
        self.sock.send('JOIN #%s\r\n') % self.room
        while True:
            data = self.sock.recv(4096)
            if data.find('PING') != -1:
                self.sock.send('PONG' + data.split()[1]+'\r\n')
            print data

    def close(self):
        self.sock.send('PART #%s\r\n') % self.room
        self.sock.send('QUIT\r\n')
        self.sock.shutdown(SHIT_RDWR)
        self.sock.close()

IRCBot('irc.psych0tik.net','hbh', 'pythonircclient',6697,1).start()

Anyone know why it might be doing this? Config problem?

Thanks in advance,
Jon
 
J

Jonathon Sisson

SSL objects use "write", not "send".

You also need to change this:

self.sock.write('NICK %s\r\n') % self.nick

to this:

self.sock.write('NICK %s\r\n' % self.nick)

If you don't, the interpreter will bomb on trying to concatenate the
return value for "write" (an integer) with the string self.nick.

Hope this helps...

Jonathon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top