Hardware Reference
In-Depth Information
section does. In addition to the functions used in Raspberry Snake, pygame provides lots of
features not used in this program, including audio playback, sprite handling for better graph-
ics and mouse control. The best place to learn about pygame's more-advanced functions is on
the official website, http://www.pygame.org/wiki/tutorials , where you can down-
load tutorials and example programs to get a handle on how things work.
Example 4: Python and Networking
So far, you have learned how Python can be used to create standalone programs, but the
language can also be used to create programs that communicate with the outside world over
a computer's network connection. This next example, written by Tom Hudson, offers a brief
glimpse of these possibilities with a tool for monitoring the users connected to an Internet
Relay Chat (IRC) channel.
As usual, create a new project in IDLE or a text editor and enter the shebang line along with
a comment describing the purpose of the program:
#!/usr/bin/env python
# IRC Channel Checker, written for the ↵
Raspberry Pi User Guide by Tom Hudson
Next, import the modules required by the program— sys , socket and time —with the fol-
lowing line:
import sys, socket, time
You used the sys and time modules previously in the Raspberry Snake program, but you
have not yet used socket . he socket module provides Python with the ability to open,
close, read from and write to network sockets—giving Python programs rudimentary net-
working capabilities. It's the socket module that provides this example with its ability to
connect to a remote IRC server.
here are some constants needed for this program to operate. Constants are like variables in
that they can have values assigned to them—but unlike variables, the value in a constant
shouldn't change. To help differentiate a constant from a variable, it's good practice to use all-
capital letters for their names—that way it's easy to see at glance whether a particular section
of the code is using a constant or a variable. Type the following two lines into the program:
RPL_NAMREPLY = '353'
RPL_ENDOFNAMES = '366'
Search WWH ::




Custom Search