Python and IRC - Can You Hear Me? (
Page 3 of 5 )
Now that we know how to connect, our next task is joining a channel and sending a message to its occupants. This is suprisingly easy.
import socket
network = 'irc.insert.a.network.here'
port = 6667
irc = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
irc.connect ( ( network, port ) )
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 :Can you hear me?\r\n' )
irc.send ( 'PART #pyirc\r\n' )
irc.send ( 'QUIT\r\n' )
irc.close()
Note that, again, the code will probably not perform the instructions we gave it, for the same reason as last time. However, the above code would ideally join the channel #pyirc and say "Can you hear me?" Let's break the code down to see how it works. The first few lines should already look familiar, and the last few lines should look familiar as well. We wrote them in the previous section. However, the "JOIN," "PRIVMGS," and "PART" lines are new. They simply join the channel, send a message to the channel and leave the channel, respectively.
To join multiple channels, just issue the "JOIN" command again. When sending a message, be sure to specify the name of the channel in the "PRIVMSG" command. You can also message another user with 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.send ( 'NICK PyIRC\r\n' )
irc.send ( 'USER PyIRC PyIRC PyIRC :Python IRC\r\n' )
irc.send ( 'PRIVMSG Jimbo :Can you hear me?\r\n' )
irc.send ( 'QUIT\r\n' )
irc.close()