Operation Manual
of the program: 353, which means a list of names follows, and 366, which means the list has ended. The if statement looks for
the first of these responses, and then uses the split function to retrieve these names and add them to the names list.
Now, the names list contains all the names received from the server in response to the program’s query. This may not be all the
names, however: until the 366 response, which signals the end of the member names, is received, the list is incomplete. That is
why the last line—names += names_list.split(‘ ‘)—is appending the newly-received names to the existing list, rather
than blanking it out entirely: each time that section of the code runs, the program is only likely to have received a sub-section of
the entire member list. To tell Python what to do when the full list has been received, enter the following lines:
if response_code == RPL_ENDOFNAMES:
# Display the names
print ‘\r\nUsers in %(channel)s:’ % irc
for name in names:
print name
names = []
This tells Python that when the 366 response has been received, it should print out the now-complete list of names to the
standard output before blanking the names list again. This last line—names = []—is important: without it, each time the loop
runs it will add users’ names to the list even though they already exist from an earlier run.
Finally, finish the program by entering the following lines:
time.sleep(irc[‘namesinterval’])
s.send(‘NAMES %(channel)s\r\n’ % irc)
This tells Python to wait the namesinterval number of seconds before sending another request for user names and beginning
the loop again. Be careful to set namesinterval to a reasonable value—if the IRC server receives too many requests in too
short a space of time, it may forcibly disconnect you for flooding.
Save the program as ircuserlist.py, and run it either by using IDLE’s Run Module option in the Run menu or from the
terminal by typing python ircuserlist.py. When the program first runs, it may take a while to connect to the server; once
connected, however, the list of names (see Figure 11-7) should refresh quickly. To quit the program, press CRTL + C.
Figure 11-7: Using Python to list users in an IRC channel
A full copy of the program listing for the IRC user list is included in Appendix A, “Python Recipes”, and on the Raspberry Pi
User Guide website at http://www.wiley.com/go/raspberrypiuserguide. Downloading the source code from the
website will save you some typing, but entering the code by hand is a good way of ensuring that you understand what each
section does.
Further Reading