Operation Manual
names in a channel, however, you need to identify yourself to the server and issue some commands using the send function of the
socket module. Type the following lines into the program:
s.send(‘NICK %(nick)s\r\n’ % user)
s.send(‘USER %(username)s %(hostname)s
%(servername)s :%(realname)s\r\n’ % user)
s.send(‘JOIN %(channel)s\r\n’ % irc)
s.send(‘NAMES %(channel)s\r\n’ % irc)
The send function works in almost exactly the same way as the print function, except that instead of printing to the standard
output—usually the terminal window or console—it sends the output through the network connection. In this case, the program
is sending strings of text to the IRC server and telling it to register the program using the nickname held in the nick variable and
the user details held in the username, hostname, servername and realname variables. Next, the program sends the
command to join the channel specified in the channel variable, and finally, it sends the command to receive the list of users in
that channel. Although this example is tailored to IRC, the same basic principle can be used to issue commands to any network
service—with modifications, this program could be used to list the files on an FTP server, or unread emails on a POP3 server.
Receiving data from the socket is a little more complicated. First, you’ll need to create an empty string variable that will act as the
receive buffer, holding data from the server as it’s received until it can be processed. Initialise the buffer by typing in the following
line:
read_buffer = ‘‘
Note that there are two single quotes after the equals sign, not one double quote.
Next, create an empty list ,which will be used to store the names of users, by typing the following line:
names = []
The list data type is the same as you used to store the locations in the Raspberry Snake game. Unlike a normal variable, it can
store multiple values—in this case, the names of users present in the IRC channel.
The next step is to create an infinite loop, during which the program will continuously query the server for user names and print
them to the screen. Start the loop by typing:
while True:
read_buffer += s.recv(1024)
The first line of the loop, following while True:, tells the socket module to receive 1,024 bytes (1 KB) of data from the IRC
server and place it into the read_buffer variable. Because the += operator is used, rather than just =, the received data will be
appended to anything already in the buffer. The value of 1024 bytes is more or less arbitrary.
The next step is to split the buffer into individual lines of text, using the following program lines:
lines = read_buffer.split(‘\r\n’)
read_buffer = lines.pop();
The first line sets the lines variable to a full line of text from the receive buffer, by using the split function to find end of line
characters—signified by \r\n. These characters only occur at the end of a line, so when the buffer has been split in this way you
know that lines contains only full-line responses from the server. The pop instruction in the second line makes sure that only full
lines are removed from the read_buffer: because responses from the server are read in 1 KB chunks, it’s likely that at any
given time the buffer will contain only fractions of a line. When that’s the case, the fraction is left in the buffer ready to receive the
remainder of the line the next time the loop runs and the next 1 KB chunk is received from the server.
At this point, the lines variable contains a list of full responses—full lines—received from the server. Type the following to
process these lines and find the names of channel participants:
for line in lines:
response = line.rstrip().split(‘ ‘, 3)
response_code = response[1]
if response_code == RPL_NAMREPLY:
names_list = response[3].split(‘:’)[1]
names += names_list.split(‘ ‘)
This runs through every line found in the lines variable, and looks for the numerical IRC response code provided by the server.
Although there are plenty of different response codes, this program is only interested in the two defined as constants at the start