Raspberry Pi A User Manual page 212

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

Advertisement

196
sends the output through the network connection. In this case, the program is sending
strings of text to the IRC server and telling it to register the program using the nickname
held in the
name
specified in the
users in that channel. Although this example is tailored to IRC, the same basic principle can
be used to issue commands to any network service—with modifications, this program could
be used to list the files on an FTP server, or unread emails on a POP3 server.
Receiving data from the socket is a little more complicated. First, you need to create an
empty string variable that will act as the receive buffer, holding data from the server as it's
received until it can be processed. Initialise the buffer by typing in the following line:
read_buffer = ''
Note that there are two single quotes after the equals sign, not one double quote.
Next, create an empty list, which will be used to store the names of users, by typing the fol-
lowing line:
names = []
The list data type is the same as you used to store the locations in the Raspberry Snake game.
Unlike a normal variable, it can store multiple values—in this case, the names of users pres-
ent in the IRC channel.
The next step is to create an infinite loop, during which the program will continuously query
the server for user names and print them to the screen. Start the loop by typing:
while True:
The first line of the loop, following
bytes (1 KB) of data from the IRC server and place it into the
the
already in the buffer. The value of 1024 bytes is more or less arbitrary.
The next step is to split the buffer into individual lines of text, using the following program
lines:
P A R T I I I
P R O G R A M M I N G W I T H T H E R A S P B E R R Y P I
variable and the user details held in the
nick
and
variables. Next, the program sends the command to join the channel
realname
variable, and finally, it sends the command to receive the list of
channel
read_buffer += s.recv(1024)
operator is used, rather than just
+=
lines = read_buffer.split('\r\n')
read_buffer = lines.pop();
, tells the
while True:
, the received data will be appended to anything
=
,
,
username
hostname
module to receive 1,024
socket
variable. Because
read_buffer
server-

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