
C H A P T E R 1 2
A N I N T R O D U C T I O N T O P Y T H O N
197
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 = []
Summary of Contents for A
Page 1: ......
Page 2: ......
Page 3: ...Raspberry Pi User Guide 2nd Edition...
Page 4: ......
Page 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Page 10: ......
Page 26: ...R A S P B E R R Y P I U S E R G U I D E S E C O N D E D I T I O N 10...
Page 28: ......
Page 29: ...Chapter 1 Meet the Raspberry Pi...
Page 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Page 56: ......
Page 57: ...Chapter 3 Linux System Administration...
Page 79: ...Chapter 4 Troubleshooting...
Page 89: ...Chapter 5 Network Configuration...
Page 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Page 122: ......
Page 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Page 140: ......
Page 141: ...Chapter 8 The Pi as a Home Theatre PC...
Page 151: ...Chapter 9 The Pi as a Productivity Machine...
Page 160: ......
Page 161: ...Chapter 10 The Pi as a Web Server...
Page 172: ......
Page 173: ...Chapter 11 An Introduction to Scratch...
Page 189: ...Chapter 12 An Introduction to Python...
Page 216: ......
Page 218: ......
Page 219: ...Chapter 13 Learning to Hack Hardware...
Page 234: ......
Page 235: ...Chapter 14 The GPIO Port...
Page 249: ...Chapter 15 The Raspberry Pi Camera Module...
Page 265: ...Chapter 16 Add on Boards...
Page 280: ......
Page 281: ...Appendix A Python Recipes...
Page 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Page 293: ...Appendix C HDMI Display Modes...