Example 4: Python And Networking - Raspberry Pi A User Manual

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

Advertisement

C H A P T E R 1 2
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
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—
lowing line:
import sys, socket, time
You used the
and
sys
have not yet used
socket
close, read from and write to network sockets—giving Python programs rudimentary net-
working capabilities. It's the
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'
A N I N T R O D U C T I O N T O P Y T H O N
modules previously in the Raspberry Snake program, but you
time
. The
module provides Python with the ability to open,
socket
module that provides this example with its ability to
socket
, where you can down-
,
and
sys
socket
time
—with the fol-
193

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Raspberry pi b

Table of Contents