IRC is becoming an increasingly popular medium for communication. In this article, Peyton McCullough explains how to make Python and IRC work together.
Our first task is to connect to an IRC network. To do this, we must first create a socket. Then, we must connect to the network, and, finally, we must complete a few short steps to become eligible to interact with other users.
Although the code demonstrates the absolute basics, it doesn't do anything special. In fact, the server you connect to might not even acknowledge the data you send it until after a few seconds – which we do not allow for in the script. Don't worry though, we will soon take a look at a fully functional script after we tackle the very basics.
The first piece of data we send to it sets our nickname. Notice how we suffix each outgoing message with a carriage return and line feed ( "\r\n" ). Take note of this because it is very important. We then specify our username ( "PyIRC" ), host name ( "PyIRC" ), server name ( "PyIRC" ) and real name ( "Python IRC" ) in the next outgoing line. Finally, we issue the "QUIT" command and close the connection.
You should also take note of the colon before "Python IRC." Colons tell the server that the attribute will possibly be made up of multiple words.
We will use and develop this skeleton throughout the remainder of the article, so it is important that you understand it.