Python and IRC - I Can Hear You! (Page 4 of 5 )
So far, we can connect to an IRC network and send a message to a given channel. We can also send a message to another user. Now we will accept messages from both the server and from other users.
Every once in a while, the server will send us a "PING" command to check on us. To stay connected, we must send the server a "PONG" command. Let's build a script that does just that.
import socket
network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
print 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' )
print data
As you can see, we've changed our skeleton a bit in this script. We have replaced the bottom section with an infinite loop. The loop receives data, and if a "PING" command is present, it replies with a "PONG" command. Feel free to test the script out.
Let's modify our script to accept messages. Messages come in a form similar to this:
:Nick!user@host PRIVMSG destination :Message
Here's an example:
:Peyton!~peyton@python.org PRIVMSG #pyrc :Hey!
In our new script, we will break down the above form of data.
import socket
network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
trash = 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: ] )
print nick + ':', message
We've added a few lines to the script. If the "PRIVMSG" command is found inside the line of data, we pull the line apart to get the nickname of the person who sent it and the message by using the split() function. Run the script, join #pyirc on the specified network and test out the script.
Now we need our script to discriminate between messages in different channels and private messages. This can be done easily by extracting the destination from the "PRIVMSG" command.
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
Test out the script like before and see the result. You should see something similar to this:
( #pyirc ) Peyton: Test
( PRIVATE ) Peyton: This is a private message.
Next: IRC Mathematics >>
More Python Articles
More By Peyton McCullough