Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

LWP - timeout

by agoth (Chaplain)
on Jan 26, 2001 at 15:28 UTC ( [id://54528]=perlquestion: print w/replies, xml ) Need Help??

agoth has asked for the wisdom of the Perl Monks concerning the following question:

Am I misunderstanding the $ua->timeout() method of LWP UserAgent? the www.richard.com in the list doesnt return but doesnt timeout either even if set to 10 seconds.
Is there a way to stop this, short of terminating the request myself??

use LWP::UserAgent; my $ua = LWP::UserAgent->new; print "Timeout : ", $ua->timeout, "\n"; $ua->timeout(10); print "Timeout : ", $ua->timeout, "\n"; my %urls = ('http://www.hotmail.com', '', 'http://www.richard.com', '', 'http://www.slashdot.org', '', 'http://doesnt.exist.com/', '', 'http://www-uk.cricket.org/', ''); are_live(keys %urls); sub are_live { for (@_) { $ua->timeout('5'); my $res = $ua->request(HTTP::Request->new(GET => $_)); $urls{$_} = ($res->is_success) ? "OK" : "NOT OK"; } } for (keys %urls) { print "URL : $_ : $urls{$_} \n"; }

Replies are listed 'Best First'.
Re: LWP - timeout
by arhuman (Vicar) on Jan 26, 2001 at 16:42 UTC
    It seems I had a similar problem...
    The fact is that if the host is unreachable, the timeout doesn't seems to work...
    Or maybe the time out is only used once the TCP connections is done...
    You could try to test if host is reachable before connecting or set your sig handler to control the time out 'by-hand' (something like (courtesy of axion on QuestionExchange) :
    #!/usr/local/bin/perl -w use strict; use IO::Socket; { my $timeout = 0; my $sock = undef; $SIG{ALRM} = sub {$timeout = 1; die}; eval { alarm (2); $sock = new IO::Socket::INET (PeerAddr => '10.1.1.18', PeerPort +=> 29, ); alarm (0); }; die "Can't open socket: timeout=$timeout\n" if ($timeout or !$sock); print "I would print to the socket now, if I knew what I was connecte +d to\n"; close ($sock); }
    ) Or even modify the LWP Timeout method...
Re: LWP - timeout
by Rune (Initiate) on Jan 26, 2001 at 19:33 UTC
    It seems that www.richard.com is not properly configured and doesn't resolve. The $ua->timeout() probably doesn't take account for DNS timeouts and AFAIR, doesn't have any options specifically for this.

    It seems to me that you'll have to live with the long timeout or perform a lookup seperately before the actual HTTP request. I'm pretty sure that there's a DNS module on CPAN that provides more control over the DNS query than LWP does.
Re: LWP - timeout (out of luck on Win32)
by dws (Chancellor) on Jan 26, 2001 at 23:03 UTC
    You don't mention what platform you're running on. If you're on Win32, you may be SOL. Under the covers, LWP uses SIGALRM (in IO::Socket) for timeouts. Unfortunately, signals aren't supported on Win32.

    See the "Quicks" page in the ActiveState FAQ for details.

    (If someone knows otherwise, please set me straight. I've never been able to get timeouts working with LPW on Win32, but perhaps there's some trick.)

      The IO::Socket modules use the timeout feature built into the select() system call to do timeouts when recving or sending data; however, in order to do a connect, they attempt to set the socket to non-blocking mode (which does not work properly without some cajoling on Win32 systems).

      For this reason, if you cannot actually connect to your peer's http port, a Win32 timeout on an LWP call will not work. If, however, you can connect to the peer's listening socket, then the timeout should work.

      Mercifully I'm on Solaris and occasionally FreeBSD,
      I know IO::Socket is installed, but i suspect the reason is as above.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54528]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2025-04-25 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.