Raspberry Pi A User Manual page 213

Hide thumbs Also See for Raspberry Pi A:
Table of Contents

Advertisement

C H A P T E R 1 2
The first line sets the
lines
function to find end of line characters—signified by
split
the end of a line, so when the buffer has been split in this way you know that
only full-line responses from the server. The
only full lines are removed from the
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
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
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:
, which means a list of names follows, and
353
statement looks for the first of these responses, and then uses the
these names and add them to the
Now, the
list contains all the names received from the server in response to the pro-
names
gram's query. This may not be all the names, however: until the
the end of the member names, is received, the list is incomplete. That is why the last line—
names += names_list.split(' ')
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 = []
A N I N T R O D U C T I O N T O P Y T H O N
variable to a full line of text from the receive buffer by using the
instruction in the second line makes sure that
pop
read_buffer
variable contains a list of full responses—full lines—received from the
lines
366
list.
names
—is appending the newly received names to the
. These characters only occur at
\r\n
: because responses from the server are
variable, and looks for the numerical IRC
, which means the list has ended. The
function to retrieve
split
response, which signals
366
contains
lines
if
197

Hide quick links:

Advertisement

Table of Contents
loading

Related Products for Raspberry Pi Raspberry Pi A

This manual is also suitable for:

Raspberry pi b

Table of Contents