1. Trang chủ
  2. » Công Nghệ Thông Tin

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

23 405 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Check if servers are up: Webping
Thể loại chapter
Định dạng
Số trang 23
Dung lượng 47,03 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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.. A place to manually enter URLs would be nice, and a display of t

Trang 1

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):

Trang 2

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:

Trang 3

## -i : Sets the autoping interval to <int>

## -f : Uses <filename> instead of webping_sites

## parsing them manually (ick)

## The standard wm specs are allowed after the , -geometry and

## -iconic being the most useful of them

The first section of the code says to use Tk, LWP::Simple, and

Getopt::Long We chose to utilize Getopt::Long so that we wouldn't have to parse any command-line options ourselves As you can see from our usage

Trang 4

statement, we've got quite a few to deal with Automode is the term we use

when the program loops and checks each web site every n minutes

## DEFAULT values may be changed by specifing cmd line options

Trang 5

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 into too much detail about how this all happens, but here is the code:

Trang 6

Notice that when we hit the Update button, we end the current automode (if

we can) This is so that the program doesn't try to do two things at once

$update_bttn->pack(-side => "left", -anchor => "w", -padx => 5);

Trang 7

$del_bttn = $button_f->Button(-text => "Delete",

Trang 8

Using a menu button like this is often a good way to get a list of items into a very small space:

$button_f->Button(-text => "Exit",

-command => side => "right",

Trang 10

-command => \&add_site)->pack(-side => 'left',

Trang 11

my $list = $list_f->Listbox(-selectmode =>

'extended',

-yscrollcommand => ['set',

$scroll]);

$scroll->configure(-command => ['yview', $list]);

$scroll->pack(-side => 'right', -fill => 'y');

$list>pack(side => 'left', expand => 'yes', fill => 'both');

Trang 12

Here is where we take advantage of a "remembering" file When the

program exits, we will save the current list of sites to this file This way, when the program is started the next time, it looks exactly as it did the last

Trang 13

time we ran it except that the program will have updated the list of sites with the current status

a bunch of flashy buttons and lists

Trang 14

This is how we always save the current state of the site list The only way to avoid running this function when exiting the application is to use the

Window Manager's close/exit/destroy commands:

Trang 15

if ($content) {

$site = " is UP (" localtime() ")";

} else {

$site = " is DOWN (" localtime() ")";

push (@down, $site);

Trang 17

}

The function ping_site( ) is called when a new site is added to update its status It is also called when in automode It checks the sites selected in the listbox ping_site( ) is where you could put in other things to happen when a site is down For instance, mail the web administrator, page the

administrator with a text message, or whatever you'd like!

sub add_site {

return if ($url eq ""); ## Do nothing, empty string

## Insert new site name into list, and make

sure we can see it

$list->insert('end', $url);

$list->see('end');

Trang 18

## Select the item so that ping_site can do all the work

Trang 19

## we just retrieved remain valid until we're done

map { $list->delete($_) } sort antinumerically

Trang 21

## end of do_automode

#################################################

When starting off in automode, do_automode( ) gets called It verifies that the list has at least one site selected, and starts the timed loop The Tk construct to do the "looping" is in the $mw->repeat( ) command The function ping_site( ) will be called every $ping_interval minutes until end_autmode( ) is called

And finally, webping looks like Figure 7-4

Figure 7-4 webping client

Trang 22

them visible by specifying -borderwidth and -relief options

3 You'll note that it looks like a lot of extra effort to declare sub {

do_search() } Doing it this way prevents any parameters from being sent to our function when it is called

4 To check to make sure you have this font family on your system, use xlsfonts If you don't have it, just pick another font you do have

5 One of the annoying things about a text widget is that when you disable it for the user, you also disable it for yourself If you want to do anything to it other than destroy it, you need to configure it back to normal

Ngày đăng: 14/12/2013, 13:15

TỪ KHÓA LIÊN QUAN