Tài liệu Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P4 - Pdf 87

Chapter 7: Graphical Examples with Perl/Tk- P4

Check if Servers Are up: webping
For the last example, we'll build a GUI interface that will allow us to check
and see if several web sites are running, at pre-specified intervals. Since this
action is very similar to the UNIX ping command, we call it webping. This
application would be useful to a web administrator who had to keep track of
many different web sites, and wanted to know when one was down or not
responding. We'll be utilizing the LWP::Simple module to actually ping
each site.
The code to check a site's status is as follows, where $site is a string
containing a standard URL (like http://www.ora.com):
$content = head($site);
if ($content) {
## Site is UP.
} else {
## Site is DOWN.
}
While that's pretty simple, we have to have some way to set $site to a URL.
It's not very efficient to have to type a new site on the command line each
time we want to verify the status of a site. In order to make our GUI useful,
we want to add some basic features to it.
A place to manually enter URLs would be nice, and a display of the sites we
have checked and their status would be useful. Having the program
automatically perform an update on each of the sites in the list every 30
minutes or so would be extremely useful. In that same vein, specifying the
interval would also be easier than editing the source code any time we
decide to change how often the ping happens. After we build a list of sites, it
would be nice for the program to remember them, and bring them up
automatically the next time we start the program.
Here's the final code, with most of the mentioned features represented:

when the program loops and checks each web site every n minutes.
## DEFAULT values -- may be changed by specifing
cmd line options.
my $site_file = "$ENV{HOME}/.webping_sites";
$ping_interval = 5;
$auto_mode = 0;
@intervals = (5, 10, 15, 20, 25, 30, 35);

sub numerically { $a <=> $b; }
sub antinumerically { $b <=> $a; }

## Parse our specific command line options first
&GetOptions("i=i" => \$ping_interval,
"f=s" => \$site_file,
"a" => \$auto_mode);

if (! grep /$ping_interval/, @intervals) {
push (@intervals, $ping_interval);
}
These segments set up stuff the program should know about. There are
default values for everything they might set on the command line. We've
declared two sorting routines to be used later on. We get the options
specified by the user (if any) to put the program in automode, add or set the
interval, and determine which file to read our list of web sites from, if not
the default file.
Next comes the meat of the GUI: setting up the window, widgets, and
callbacks. webping does more complicated things than xword, so it will take
quite a bit more effort to set it all up. No matter what it does, though, it all
looks pretty much the same: creating buttons, assigning functions for them
to call, and placing the widgets in a sensible order via pack. We won't go

&delete_site });
$del_bttn->pack(-side => "left",
-anchor => 'w',
-padx => 10);

$automode_bttn = $button_f->Button(-text => "Start
Automode",
-command =>
\&do_automode);
$automode_bttn->pack(-side => 'left');

$button_f->Label(-text => "Interval: ")->pack(-side
=> "left");

## Create a psuedo pop-up menu using Menubutton
$interval_mb = $button_f->Menubutton(-indicatoron
=> 1,
-borderwidth => 2,
-relief => "raised");
$interval_mb->pack(-side => "left");

$interval_mb->configure(-menu => $interval_mb-
>Menu(-tearoff => 0),
-textvariable => \$ping_interval);
map { $interval_mb->radiobutton(-label => $_,
-variable => \$ping_interval,
-value => $_,
-indicatoron => 0) } sort
numerically @intervals;
Using a menu button like this is often a good way to get a list of items into a


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status