Hardware Reference
In-Depth Information
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 bufer will contain only
fractions of a line. When that's the case, the fraction is left in the bufer 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 ind 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(' ')
his 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 diferent response codes,
this program is only interested in the two deined as constants at the start of the program:
353 , which means a list of names follows, and 366 , which means the list has ended. he if
statement looks for the irst 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. his 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. hat 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