Python and IRC - Hello IRC World (
Page 2 of 5 )
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.
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 ( 'QUIT\r\n' )
irc.close()
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.