
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
193
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
. The
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.
There 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’
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...