For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > Re: Performance issues with LWP? (off-topic?)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Performance issues with LWP? (off-topic?)
Mahesha

2004-03-29, 7:36 pm

Prabh wrote:

> Hello all,
> In my task I need to check if the web server at a known URL is up and
> available. If its not, I need to send a mail to the system admins that
> the server is down and if its available then get the URL's contents.
> The simplest way I could think of doing this was with LWP.
>
>
> use LWP::Simple ;
> my $chkUrl = get("http://abc.xyz.com");
> if ( ! $chkUrl )
> {
> &mail_the_Admins_the_server_is_down() ;
> die "Cant connect: Check the URL or the Server is down\n" ;

------------------------^^^^^^^^^^^^^

In this example, the URL is hard-coded. I am assuming it is a valid URL
in the actual application, in which case 'Check the URL' will not apply.

If, however, the URL comes as an argument from a user, admins will get
an email every time a user mis-types the URL. That could be an annoyance
to the admins. No?
--
M
Myron Turner

2004-03-29, 8:36 pm

On Mon, 29 Mar 2004 15:48:29 -0800, Mahesha
<mahesha-at-mahesha-dot-net> wrote:

>Prabh wrote:
>
>------------------------^^^^^^^^^^^^^
>
>In this example, the URL is hard-coded. I am assuming it is a valid URL
>in the actual application, in which case 'Check the URL' will not apply.
>
>If, however, the URL comes as an argument from a user, admins will get
>an email every time a user mis-types the URL. That could be an annoyance
>to the admins. No?
>--
>M

You'' have to use LWP::UserAgent and HTTP::Response, where you can
check the response message and response code. It's not as complicated
as it first appears. There's a simple example in the Synopsis to
LWP::UserAgent that works fine.

Try this:

require LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->timeout(10);
my $response = $ua->get('http://bad.url.org/');
if ($response->is_success) {
print $response->content; # or whatever
}
else {
die $response->status_line;
}

The error message will read:
500 Can't connect to bad.url.org:80 (Bad hostname 'bad.url.org') at
script.pl line n.


Myron Turner
www.room535.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com