February 23rd, 2005, 07:35 AM
-
Python And IRC
I read this article from here:
http://www.devshed.com/c/a/Python/Python-and-IRC/
For some reason, I can't get the bot to connect to an irc channel. The syntax is exactly the same as the examples.
Is it possible that because my router does not accept pings it won't connect? Any way to debug it? Anybody get that example to work?
Help me obi-wan kenobi...your my only hope.
I'll learn this stuff someday.
February 23rd, 2005, 12:16 PM
-
post your code and the error you're getting
February 23rd, 2005, 12:41 PM
-
No errors, it simply does not connect to the channel. I was trying the code from the article I linked to. It is this one:
Code:
import socket
network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
irc.recv ( 4096 )
irc.send ( 'NICK PyIRC\r\n' )
irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
irc.send ( 'JOIN #pyirc\r\n' )
irc.send ( 'PRIVMSG #pyirc :Hello.\r\n' )
while True:
data = irc.recv ( 4096 )
if data.find ( 'PING' ) != -1:
irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
elif data.find ( 'PRIVMSG' ) != -1:
nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
message = ':'.join ( data.split ( ':' ) [ 2: ] )
destination = ''.join ( data.split ( ':' ) [ :2 ] ).split ( ' ' ) [ -2 ]
if destination == 'PyIRC':
destination = 'PRIVATE'
print '(', destination, ')', nick + ':', message
I'll learn this stuff someday.
February 23rd, 2005, 03:16 PM
-
When running this script I actually get a Traceback:
Code:
Traceback (most recent call last):
File "./test.py", line 8, in ?
irc.connect ( ( network, port ) )
File "<string>", line 1, in connect
socket.gaierror: (7, 'No address associated with nodename')
Probably because the IRC chanel your trying to connect to is "irc.insert.a.network.here". Try another IRC network. I tried a few and It just seemed to hang however this might not be because if hasn't connected.
I'll give the IRC article a read though and let you know if anything jumps out.
Hope this helps,
Mark,
February 23rd, 2005, 04:49 PM
-
Yeah sorry, I actually did replace everything with a correct irc network and channel. Just like you, it hangs and does not connect.
Thanks.
I'll learn this stuff someday.
February 23rd, 2005, 05:10 PM
-
Hey Shiner,
I concluded that it does connect by adding a few print statements, mainly inside the PING if block which showed the client and server talking.
Try connecting with an IRC client and see if you see your bot in the room. If you do, send it a few private messages
. I don't use IRC so don't know any IRC network to test this with but it seems like it should work.
Have fun,
Mark.
February 23rd, 2005, 05:34 PM
-
Well, the bot is not connecting to the channel at all. I did stumble upon some code you wrote in another thread:
Code:
#!/usr/bin/env python
import socket
class IRC:
def __init__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self):
try:
self.sock.connect((self.host, self.port))
self.sock.send('NICK %s\n\r' % self.nick)
self.sock.send('USER localhost localhost localhost : Python Powered\n\r')
self.sock.send('JOIN %s\n\r' % self.room)
self.sock.send('PRIVMSG %s : Python Powered\n\r' % self.room)
self.sock.send('NAMES\n\r')
while True:
print self.recieve()
except:
print 'Error Occured'
def recieve(self):
line = self.sock.recv(self.byte)
if line.startswith('PING'):
self.sock.send('PONG %s\n\r' % line)
print line
return 'PONG has been sent'
else:
return line
if __name__ == '__main__':
pybot = IRC()
pybot.host = 'irc.us.gamesurge.net'
pybot.room = '#channel'
pybot.nick = 'CheckMan'
pybot.port = 6667
pybot.byte = 1025
pybot.connect()
I did have to change the self.sock.send line in order for it to connect. You can see the changes above.
Now, this code does connect to irc.us.gamesurge.net but it does not connect to the channel(I do have a real channel in the code I'm using)
What happens is once it connects to irc.gamesurge.net it spits out a bunch of IRC stuff(i.e. motd, server rules, etc.) and then has three lines that end with 'Unknown Command'
Any idea what 3 commands in the script you made are 'Unkown"?
I'll learn this stuff someday.
February 23rd, 2005, 05:48 PM
-
(just a thought): change the \n\r to \r\n...that *MAY* help...
February 23rd, 2005, 06:36 PM
-
I changed them but got the same thing.
I'll learn this stuff someday.
February 23rd, 2005, 11:10 PM
-
Maybe you could add a time.sleep in between the NICK command and the other commands, giving it a few seconds to send the MOTD, etc.
March 5th, 2005, 04:30 PM
-
Hello,
Shiner, the network you're trying to connect to doesn't recognize the script's registration until it's too late. However, we can easily work around that:
Code:
import socket
network = 'irc.us.gamesurge.net'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
irc.recv ( 4096 )
irc.send ( 'NICK PyIRC\r\n' )
irc.send ( 'USER Peyton PyIRC PyIRC :Python IRC\r\n' )
channelSwitch = 0
while True:
data = irc.recv ( 4096 )
if channelSwitch == 0:
irc.send ( 'JOIN #pyirc\r\n' )
if data.find ( 'PING' ) != -1:
irc.send ( 'PONG ' + data.split() [ 1 ] + '\r\n' )
elif data.find ( 'PRIVMSG' ) != -1:
nick = data.split ( '!' ) [ 0 ].replace ( ':', '' )
message = ':'.join ( data.split ( ':' ) [ 2: ] )
destination = ''.join ( data.split ( ':' ) [ :2 ] ).split ( ' ' ) [ -2 ]
if destination == 'PyIRC':
destination = 'PRIVATE'
print '(', destination, ')', nick + ':', message
elif data.find ( '#pyirc' ) != -1:
channelSwitch = 1
-Peyton
March 5th, 2005, 09:36 PM
-
Wow thanks a lot! It actually works now. Believe it or not I actually used the Twisted irc framework to get my bot working but now I can dump that and re-write my program with this.
Thanks!
I'll learn this stuff someday.