Hardware Reference
In-Depth Information
his 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. his 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, inish the program by entering the following lines:
time.sleep(irc['namesinterval'])
s.send('NAMES %(channel)s\r\n' % irc)
his 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
looding
.
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 irst runs, it may take a while to connect to the server; once connected, how-
ever, 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