timeout

timeout

am 08.11.2006 15:37:36 von orasnita

Hi,

I am trying to create a very simple www client that times out after 5
seconds. I have tried the 2 examples below, but none of them are working.
They don't time out even though the target site cannot be found.
I think they time out after some more time.

I have ran them under Windows 2000 with ActivePerl 5.8.8.
Is it possible to do what I want?

Thanks.

use strict;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new(-timeout => 5);

print $ua->get("http://www.presaromana.ro/")->content;

__END__

my $req = HTTP::Request->new("GET", "http://www.presaromana.ro/");
my $res = $ua->request($req);

print $res->content();


Teddy

Re: timeout

am 08.11.2006 16:36:03 von edaly

From the LWP::UserAgent perldoc at

http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserA gent.pm

$ua->timeout( $secs )
Get/set the timeout value in seconds. The default timeout() value is 180
seconds, i.e. 3 minutes.

So in your first example, it should be:

use strict;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->timeout(5);

print $ua->get("http://www.presaromana.ro/")->content;

____________________________________________________________
Eamon Daly



----- Original Message -----
From: "Octavian Rasnita"
To:
Sent: Wednesday, November 08, 2006 8:37 AM
Subject: timeout


: Hi,
:
: I am trying to create a very simple www client that times out after 5
: seconds. I have tried the 2 examples below, but none of them are working.
: They don't time out even though the target site cannot be found.
: I think they time out after some more time.
:
: I have ran them under Windows 2000 with ActivePerl 5.8.8.
: Is it possible to do what I want?
:
: Thanks.
:
: use strict;
: use LWP::UserAgent;
:
: my $ua = LWP::UserAgent->new(-timeout => 5);
:
: print $ua->get("http://www.presaromana.ro/")->content;
:
: __END__
:
: my $req = HTTP::Request->new("GET", "http://www.presaromana.ro/");
: my $res = $ua->request($req);
:
: print $res->content();
:
:
: Teddy
:

RE: timeout

am 08.11.2006 19:22:33 von ted.behling

True. Or, Octavian, just leave out the hyphen from your timeout
parameter, as in:

my $ua =3D LWP::UserAgent->new(timeout =3D> 5);

Ted Behling
ISP Systems Analyst, Hargray Communications
ted.behling@htc.hargray.com

-----Original Message-----
From: Eamon Daly [mailto:edaly@nextwavemedia.com]=20
Sent: Wednesday, November 08, 2006 10:36 AM
To: libwww@perl.org
Cc: Octavian Rasnita
Subject: Re: timeout

From the LWP::UserAgent perldoc at

http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserA gent.pm

$ua->timeout( $secs )
Get/set the timeout value in seconds. The default timeout() value is
180 seconds, i.e. 3 minutes.

So in your first example, it should be:

use strict;
use LWP::UserAgent;

my $ua =3D LWP::UserAgent->new;
$ua->timeout(5);

print $ua->get("http://www.presaromana.ro/")->content;

____________________________________________________________
Eamon Daly



----- Original Message -----
From: "Octavian Rasnita"
To:
Sent: Wednesday, November 08, 2006 8:37 AM
Subject: timeout


: Hi,
:
: I am trying to create a very simple www client that times out after 5
: seconds. I have tried the 2 examples below, but none of them are
working.
: They don't time out even though the target site cannot be found.
: I think they time out after some more time.
:
: I have ran them under Windows 2000 with ActivePerl 5.8.8.
: Is it possible to do what I want?
:
: Thanks.
:
: use strict;
: use LWP::UserAgent;
:
: my $ua =3D LWP::UserAgent->new(-timeout =3D> 5);
:
: print $ua->get("http://www.presaromana.ro/")->content;
:
: __END__
:
: my $req =3D HTTP::Request->new("GET", "http://www.presaromana.ro/");
: my $res =3D $ua->request($req);
:
: print $res->content();
:
:
: Teddy
:=20

Re: timeout

am 08.11.2006 23:09:15 von orasnita

Hi,

I've also tried without that "-" before "timeout", but it doesn't work. I
have also tried with $ua->timeout(5), but it doesn't work either.

Here is the test script and its result:

use strict;
use LWP::UserAgent;

my $begin = time();

my $ua = LWP::UserAgent->new();
$ua->timeout(5);

print $ua->get("http://www.presaromana.ro/")->content;

print time() - $begin;

And the result is:
500 Can't connect to www.presaromana.ro:80 (connect: Unknown error)
23

So it takes 23 seconds instead of timing out after 5.

Is it a bug in LWP, or a limitation of Windows, or I am doing something
wrong?

Thank you.

Teddy

----- Original Message -----
From: "Ted Behling"
To: "Octavian Rasnita" ;
Sent: Wednesday, November 08, 2006 8:22 PM
Subject: RE: timeout


True. Or, Octavian, just leave out the hyphen from your timeout
parameter, as in:

my $ua = LWP::UserAgent->new(timeout => 5);

Ted Behling
ISP Systems Analyst, Hargray Communications
ted.behling@htc.hargray.com

-----Original Message-----
From: Eamon Daly [mailto:edaly@nextwavemedia.com]
Sent: Wednesday, November 08, 2006 10:36 AM
To: libwww@perl.org
Cc: Octavian Rasnita
Subject: Re: timeout

From the LWP::UserAgent perldoc at

http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserA gent.pm

$ua->timeout( $secs )
Get/set the timeout value in seconds. The default timeout() value is
180 seconds, i.e. 3 minutes.

So in your first example, it should be:

use strict;
use LWP::UserAgent;

my $ua = LWP::UserAgent->new;
$ua->timeout(5);

print $ua->get("http://www.presaromana.ro/")->content;

____________________________________________________________
Eamon Daly



----- Original Message -----
From: "Octavian Rasnita"
To:
Sent: Wednesday, November 08, 2006 8:37 AM
Subject: timeout


: Hi,
:
: I am trying to create a very simple www client that times out after 5
: seconds. I have tried the 2 examples below, but none of them are
working.
: They don't time out even though the target site cannot be found.
: I think they time out after some more time.
:
: I have ran them under Windows 2000 with ActivePerl 5.8.8.
: Is it possible to do what I want?
:
: Thanks.
:
: use strict;
: use LWP::UserAgent;
:
: my $ua = LWP::UserAgent->new(-timeout => 5);
:
: print $ua->get("http://www.presaromana.ro/")->content;
:
: __END__
:
: my $req = HTTP::Request->new("GET", "http://www.presaromana.ro/");
: my $res = $ua->request($req);
:
: print $res->content();
:
:
: Teddy
:

Re: timeout

am 08.11.2006 23:22:31 von chris.boyce

This is not so much a solution, as it is an alternative to the LWP
timeout...

I've had good luck just using an eval block around the request, and then
making a simple signal catcher to do something if the alarm times out:

use LWP::UserAgent;
use HTTP::Cookies;
use HTTP::Headers;
use HTTP::Request;
use HTTP::Response;

$SIG{ALRM} = sub { die "timeout\n"; };

$timeout=5;

eval {
# Set the alarm clock for timeout seconds
alarm($timeout);

# Run the request...
$http_response = $agent->request($request);

# If we're here, then the request didn't get zapped by the alarm...
# reset the alarm to nada.
alarm(0);
};

The word "timeout" will appear in the response if the alarm goes off.
Sorry if this is crude, but it works great.


-Chris


On Thu, 2006-11-09 at 00:09 +0200, Octavian Rasnita wrote:
> Hi,
>
> I've also tried without that "-" before "timeout", but it doesn't work. I
> have also tried with $ua->timeout(5), but it doesn't work either.
>
> Here is the test script and its result:
>
> use strict;
> use LWP::UserAgent;
>
> my $begin = time();
>
> my $ua = LWP::UserAgent->new();
> $ua->timeout(5);
>
> print $ua->get("http://www.presaromana.ro/")->content;
>
> print time() - $begin;
>
> And the result is:
> 500 Can't connect to www.presaromana.ro:80 (connect: Unknown error)
> 23
>
> So it takes 23 seconds instead of timing out after 5.
>
> Is it a bug in LWP, or a limitation of Windows, or I am doing something
> wrong?
>
> Thank you.
>
> Teddy
>
> ----- Original Message -----
> From: "Ted Behling"
> To: "Octavian Rasnita" ;
> Sent: Wednesday, November 08, 2006 8:22 PM
> Subject: RE: timeout
>
>
> True. Or, Octavian, just leave out the hyphen from your timeout
> parameter, as in:
>
> my $ua = LWP::UserAgent->new(timeout => 5);
>
> Ted Behling
> ISP Systems Analyst, Hargray Communications
> ted.behling@htc.hargray.com
>
> -----Original Message-----
> From: Eamon Daly [mailto:edaly@nextwavemedia.com]
> Sent: Wednesday, November 08, 2006 10:36 AM
> To: libwww@perl.org
> Cc: Octavian Rasnita
> Subject: Re: timeout
>
> From the LWP::UserAgent perldoc at
>
> http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserA gent.pm
>
> $ua->timeout( $secs )
> Get/set the timeout value in seconds. The default timeout() value is
> 180 seconds, i.e. 3 minutes.
>
> So in your first example, it should be:
>
> use strict;
> use LWP::UserAgent;
>
> my $ua = LWP::UserAgent->new;
> $ua->timeout(5);
>
> print $ua->get("http://www.presaromana.ro/")->content;
>
> ____________________________________________________________
> Eamon Daly
>
>
>
> ----- Original Message -----
> From: "Octavian Rasnita"
> To:
> Sent: Wednesday, November 08, 2006 8:37 AM
> Subject: timeout
>
>
> : Hi,
> :
> : I am trying to create a very simple www client that times out after 5
> : seconds. I have tried the 2 examples below, but none of them are
> working.
> : They don't time out even though the target site cannot be found.
> : I think they time out after some more time.
> :
> : I have ran them under Windows 2000 with ActivePerl 5.8.8.
> : Is it possible to do what I want?
> :
> : Thanks.
> :
> : use strict;
> : use LWP::UserAgent;
> :
> : my $ua = LWP::UserAgent->new(-timeout => 5);
> :
> : print $ua->get("http://www.presaromana.ro/")->content;
> :
> : __END__
> :
> : my $req = HTTP::Request->new("GET", "http://www.presaromana.ro/");
> : my $res = $ua->request($req);
> :
> : print $res->content();
> :
> :
> : Teddy
> :
>
>
>
--
Chris Boyce

Re: timeout

am 09.11.2006 07:51:53 von orasnita

Hi,

I have also tried your suggestion using alarm() but as I thought, it doesn't
seem to work. Have you tried it under Windows? As far as I know alarm()
doesn't work under Windows.

But you gave me an idea to try using a Windows Timer event. I will see if it
works that way.

Teddy

----- Original Message -----
From: "Chris Boyce"
To: "Octavian Rasnita"
Cc: "Ted Behling" ;
Sent: Thursday, November 09, 2006 12:22 AM
Subject: Re: timeout


> This is not so much a solution, as it is an alternative to the LWP
> timeout...
>
> I've had good luck just using an eval block around the request, and then
> making a simple signal catcher to do something if the alarm times out:
>
> use LWP::UserAgent;
> use HTTP::Cookies;
> use HTTP::Headers;
> use HTTP::Request;
> use HTTP::Response;
>
> $SIG{ALRM} = sub { die "timeout\n"; };
>
> $timeout=5;
>
> eval {
> # Set the alarm clock for timeout seconds
> alarm($timeout);
>
> # Run the request...
> $http_response = $agent->request($request);
>
> # If we're here, then the request didn't get zapped by the alarm...
> # reset the alarm to nada.
> alarm(0);
> };
>
> The word "timeout" will appear in the response if the alarm goes off.
> Sorry if this is crude, but it works great.
>
>
> -Chris
>
>
> On Thu, 2006-11-09 at 00:09 +0200, Octavian Rasnita wrote:
> > Hi,
> >
> > I've also tried without that "-" before "timeout", but it doesn't work.
I
> > have also tried with $ua->timeout(5), but it doesn't work either.
> >
> > Here is the test script and its result:
> >
> > use strict;
> > use LWP::UserAgent;
> >
> > my $begin = time();
> >
> > my $ua = LWP::UserAgent->new();
> > $ua->timeout(5);
> >
> > print $ua->get("http://www.presaromana.ro/")->content;
> >
> > print time() - $begin;
> >
> > And the result is:
> > 500 Can't connect to www.presaromana.ro:80 (connect: Unknown error)
> > 23
> >
> > So it takes 23 seconds instead of timing out after 5.
> >
> > Is it a bug in LWP, or a limitation of Windows, or I am doing something
> > wrong?
> >
> > Thank you.
> >
> > Teddy
> >
> > ----- Original Message -----
> > From: "Ted Behling"
> > To: "Octavian Rasnita" ;
> > Sent: Wednesday, November 08, 2006 8:22 PM
> > Subject: RE: timeout
> >
> >
> > True. Or, Octavian, just leave out the hyphen from your timeout
> > parameter, as in:
> >
> > my $ua = LWP::UserAgent->new(timeout => 5);
> >
> > Ted Behling
> > ISP Systems Analyst, Hargray Communications
> > ted.behling@htc.hargray.com
> >
> > -----Original Message-----
> > From: Eamon Daly [mailto:edaly@nextwavemedia.com]
> > Sent: Wednesday, November 08, 2006 10:36 AM
> > To: libwww@perl.org
> > Cc: Octavian Rasnita
> > Subject: Re: timeout
> >
> > From the LWP::UserAgent perldoc at
> >
> > http://search.cpan.org/~gaas/libwww-perl-5.805/lib/LWP/UserA gent.pm
> >
> > $ua->timeout( $secs )
> > Get/set the timeout value in seconds. The default timeout() value is
> > 180 seconds, i.e. 3 minutes.
> >
> > So in your first example, it should be:
> >
> > use strict;
> > use LWP::UserAgent;
> >
> > my $ua = LWP::UserAgent->new;
> > $ua->timeout(5);
> >
> > print $ua->get("http://www.presaromana.ro/")->content;
> >
> > ____________________________________________________________
> > Eamon Daly
> >
> >
> >
> > ----- Original Message -----
> > From: "Octavian Rasnita"
> > To:
> > Sent: Wednesday, November 08, 2006 8:37 AM
> > Subject: timeout
> >
> >
> > : Hi,
> > :
> > : I am trying to create a very simple www client that times out after 5
> > : seconds. I have tried the 2 examples below, but none of them are
> > working.
> > : They don't time out even though the target site cannot be found.
> > : I think they time out after some more time.
> > :
> > : I have ran them under Windows 2000 with ActivePerl 5.8.8.
> > : Is it possible to do what I want?
> > :
> > : Thanks.
> > :
> > : use strict;
> > : use LWP::UserAgent;
> > :
> > : my $ua = LWP::UserAgent->new(-timeout => 5);
> > :
> > : print $ua->get("http://www.presaromana.ro/")->content;
> > :
> > : __END__
> > :
> > : my $req = HTTP::Request->new("GET", "http://www.presaromana.ro/");
> > : my $res = $ua->request($req);
> > :
> > : print $res->content();
> > :
> > :
> > : Teddy
> > :
> >
> >
> >
> --
> Chris Boyce
>
>