Hardware Reference
In-Depth Information
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 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 pro-
gram'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 = []
Search WWH ::




Custom Search