in reply to Catching time-outs

Have a look at the attributes of the new() method for LWP::UserAgent. You'll find that the final attribute is.......timeout.

There is also the timeout() method found in the LWP::UserAgent module.

The POD for the module states:

$ua->timeout( $secs )

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

The requests is aborted if no activity on the connection to the server is observed for timeout seconds. This means that the time it takes for the complete transaction and the request() method to actually return might be longer.

I hope this helps!


Dave

Replies are listed 'Best First'.
Re: Re: Catching time-outs
by coldfingertips (Pilgrim) on Mar 29, 2004 at 17:01 UTC
    I've read that already and I know how to setup a custom timeout. My question is, how can I trap it so IF it times out MY error message will be displayed? It doesn't show you that.
      Did you also read the part of the docs that says the return value of the get() method is a response object, and to see HTTP::Response for details?

      In HTTP::Response, there is an is_success() and an is_error() method. And again we're directed somewhere else: see HTTP::Status for the meaning of these methods...

      RC_REQUEST_TIMEOUT is one of the possible response codes.


      Dave

        I tried using this and it's not working. I set a very small timeout hoping it'd catch everything (just for a test) and some still took over 20 seconds and didn't say it timed out.
        use LWP::Simple qw(!head); use LWP::UserAgent; use HTML::LinkExtor; use URI::URL; my $ua = LWP::UserAgent->new; my $p = HTML::LinkExtor->new; $ua->timeout(3); my $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])}); foreach(@url) { $url = "$_"; my $req = HTTP::Request->new(POST => "$url"); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); my $res = $ua->request($req); if ($res->status_line =~ /404/ || $res->status_line =~ /401/) { print "FAILED!"; } elsif ($res->status_line =~ /408/) { print "TIMED OUT"; } else { print "PASSED!!" } }
        No, I didn't see that which leads to one final question. That is HTTP::Status, I am using LWP Simple and UserAgent. Does LWP::Simple use HTTP::Status, is this how it checks for error codes? If not, I don't see how I could use a UA-TIMEOUT and have HTTP::Status catch it.

        Thanks for your help!! This is very promising!

      If you die() or exit () in an eval block, the error goes to $! (or is it $@?) and the eval returns false. It's like Java's try/catch. If you die on not having success you can test for that. If, on the other hand, you mean that LWP::UserAgent is printing to STDERR, and you want to stop it, just redirect STDERR -- i.e. open ("STDERR", "> ./error").


      Want to support the EFF and FSF by buying cool stuff? Click here.