Hardware Reference
In-Depth Information
Receiving data from the socket is a little more complicated. First, you'll need to create an
empty string variable that will act as the receive bufer , holding data from the server as it's
received until it can be processed. Initialise the bufer 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 = []
he 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.
he next step is to create an ininite 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:
read_buffer += s.recv(1024)
he irst line of the loop, following while True: , tells the socket module to receive 1,024
bytes (1 KB) of data from the IRC server and place it into the read_buffer variable. Because
the += operator is used, rather than just = , the received data will be appended to anything
already in the bufer. he value of 1024 bytes is more or less arbitrary.
he next step is to split the bufer into individual lines of text, using the following program
lines:
lines = read_buffer.split('\r\n')
read_buffer = lines.pop();
he irst line sets the lines variable to a full line of text from the receive bufer, by using the
split function to ind end of line characters—signiied by \r\n . hese characters only occur
at the end of a line, so when the bufer has been split in this way you know that lines con-
tains only full-line responses from the server. he pop instruction in the second line makes
Search WWH ::




Custom Search