Danh mục

Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P4

Số trang: 23      Loại file: pdf      Dung lượng: 47.03 KB      Lượt xem: 7      Lượt tải: 0    
tailieu_vip

Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu web client programming with perl-chapter 7: graphical examples with perl/tk- p4, công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P4 Chapter 7: Graphical Examples with Perl/Tk- P4Check if Servers Are up: webpingFor the last example, well build a GUI interface that will allow us to checkand see if several web sites are running, at pre-specified intervals. Since thisaction is very similar to the UNIX ping command, we call it webping. Thisapplication would be useful to a web administrator who had to keep track ofmany different web sites, and wanted to know when one was down or notresponding. Well be utilizing the LWP::Simple module to actually pingeach site.The code to check a sites status is as follows, where $site is a stringcontaining a standard URL (like http://www.ora.com):$content = head($site);if ($content) { ## Site is UP.} else { ## Site is DOWN.}While thats pretty simple, we have to have some way to set $site to a URL.Its not very efficient to have to type a new site on the command line eachtime 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 wehave checked and their status would be useful. Having the programautomatically perform an update on each of the sites in the list every 30minutes or so would be extremely useful. In that same vein, specifying theinterval would also be easier than editing the source code any time wedecide to change how often the ping happens. After we build a list of sites, itwould be nice for the program to remember them, and bring them upautomatically the next time we start the program.Heres the final code, with most of the mentioned features represented:#!/usr/bin/perl -w######################################################################### Webping: A program that will detect and reportwhether a web site is up.## usage: webping [ -a ] [ -i ] [ -f ] [-- [ -geometry...]]## -a : starts prog in autoping mode frombeginning.## -i : Sets the autoping interval to ## -f : Uses instead of .webping_sitesas site list## -- is necessary to separate webpings optionsfrom the Window## Manager options. Allows us to utilizeGetOptions instead of## parsing them manually (ick).## The standard wm specs are allowed after the --, -geometry and## -iconic being the most useful of them.#######################################################################use Tk;use LWP::Simple;use Getopt::Long;The first section of the code says to use Tk, LWP::Simple, andGetopt::Long. We chose to utilize Getopt::Long so that we wouldnt have toparse any command-line options ourselves. As you can see from our usagestatement, weve got quite a few to deal with. Automode is the term we usewhen the program loops and checks each web site every n minutes.## DEFAULT values -- may be changed by specifingcmd 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 aredefault values for everything they might set on the command line. Wevedeclared two sorting routines to be used later on. We get the optionsspecified by the user (if any) to put the program in automode, add or set theinterval, and determine which file to read our list of web sites from, if notthe default file.Next comes the meat of the GUI: setting up the window, widgets, andcallbacks. webping does more complicated things than xword, so it will takequite a bit more effort to set it all up. No matter what it does, though, it alllooks pretty much the same: creating buttons, assigning functions for themto call, and placing the widgets in a sensible order via pack. We wont gointo too much detail about how this all happens, but here is the code:my $mw = MainWindow->new;$mw->title(Web Ping);$mw->CmdLine; ## parse -geometry and etc cmd lineoptions.$frame1 = $mw->Frame;$frame1->pack(side => bottom, -anchor => n, -expand => n, -fill => x);## Create frame for buttons along the bottom of thewindowmy $button_f = $frame1->Frame(-borderwidth => 2, -relief => ridge);$button_f->pack(-side => top, -anchor => n, -expand => n, -fill => x);$update_bttn = $button_f->Button(-text => Update, -state => disabled, -command => sub {&end_automode; &ping_site });Notice that when we hit the Update button, we end the current automode (ifwe can). This is so that the program doesnt try to do two things at once.$update_bttn->pack(-side => left, -anchor => w,-padx => 5);$del_bttn = $button_f->Button(-text => Delete, -state => disabled, -command => sub {&delete_site });$del_bttn->pack(-side => left, -anchor => w, -padx => 10);$automode_bttn = $button_f->Button(-text => StartAutomode, -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, ...

Tài liệu được xem nhiều: