Updating Webcam Images
By Randal L. Schwartz
Sometimes, when I'm on the road a lot giving my Perl trainings at Fortune 100 companies all across the United States, I get a little homesick. Luckily, there are a few live Webcams set up that allow me to glimpse the current state of my hometown of Portland, Oregon.
One Website with live cameras is operated by the local television station, KGW. They provide eight Webcams from around Oregon and Portland (including one very close to my house), and they're updated every two or three minutes. In the past, I used to keep a browser on the instructor workstation (visible only to me) open on one of these views, and hit Reload whenever I wanted a new view.
Then I thought, Hey, why should I have to hit Reload? I've got Perl! So I crafted a CGI script that visits KGW's Web site every 30 seconds, and sends a new image whenever the picture is updated. This script is interesting because it combines three things I haven't used before in one program:
- Multipart server-push output.
- Both CGI and LWP modules.
- The concept of an Non-Parsed Header (NPH) script.
I've looked at #1 and #2 individually in previous columns; this month, I'll look at #3. An NPH script is different from a regular CGI script. When a regular CGI script prints to STDOUT, the HTTP server takes its output and packages it into a valid HTTP message. If the CGI script puts header information into the output stream, the server parses this and places it into the HTTP header.