
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
195
clear that the user is a program rather than a real person. Do the same with
username
, and
fill in the
realname
variable with a descriptive message about whom the bot belongs to. The
hostname
and
servername
variables can be left set to
localhost
, or altered to match
your Internet address.
The
socket
module requires the user to create a socket
object
. This object provides network
connectivity to the rest of the program. Create the socket object by typing in the following
line:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Next, you need to tell the program to try connecting to the IRC server specified in the vari-
ables at the start of the program. Type the following lines:
print ‘Connecting to %(host)s:%(port)s...’ % irc
try:
s.connect((irc[‘host’], irc[‘port’]))
except socket.error:
print ‘Error connecting to IRC server
↵
%(host)s:%(port)s’ % irc
sys.exit(1)
The
try
and
except
commands are included in this code for
error handling
. If the system
fails to connect to the server—because the Pi isn’t connected to the Internet, for example, or
because the server is down for maintenance—the program will print an error message and
gracefully exit. The
s.connect
line tells the socket module to try connecting to the IRC
server, using the
host
and
port
variables held in the
irc
dict.
If the program doesn’t quit from the exception, it has successfully connected to the IRC
server. Before you can get a list of names in a channel, however, you need to identify yourself
to the server and issue some commands using the
send
function of the
socket
module.
Type the following lines into the program:
s.send(‘NICK %(nick)s\r\n’ % user)
s.send(‘USER %(username)s %(hostname)s
↵
%(servername)s :%(realname)s\r\n’ % user)
s.send(‘JOIN %(channel)s\r\n’ % irc)
s.send(‘NAMES %(channel)s\r\n’ % irc)
The
send
function works in almost exactly the same way as the
function, except that
instead of printing to the standard output—usually the terminal window or console—it
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...