Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How to catch Net::FTP timeouts ?

by Brutha (Friar)
on Sep 24, 2004 at 11:51 UTC ( #393466=perlquestion: print w/replies, xml ) Need Help??

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

Dear fellows, I use the Net::FTP module to retrieve files from a mainframe machine. Since the provider changed, timeouts occur during communications, here during an getfile via FTP. The code is nothing special, straight from the pod:
use Net::FTP; ... $ftp->get("that.file"); ...
I get the following message when I turn FTP debug on:
Net::FTP=GLOB(0x??????)>>> PORT xxx,xxx,xxx,xxx,198 Net::FTP=GLOB(0x??????)<<< Port request OK. Net::FTP=GLOB(0x??????)>>> RETR FFFFF.XXX Net::FTP=GLOB(0x??????)<<< 125 Sending data set Timeout at ... Net/ line 471 Net::FTP=GLOB(0x??????)>>> QUIT Net::FTP=GLOB(0x??????)<<< 250 Transfer completed successfully
How do I handle the Timeout error?

Net::FTP seems to have no code to deal with it. I had a short look at the underlying modules Net::Cmd, IO::Socket etc, but could not even find, where the Timeout method is generated.

As there is conversion between the mainframe and the windows client it is not easy to check filesizes to decide, wether the download retrieved the complete or just part of the file.

Any hints, links, tips or help? I use ActiveState Perl 5.6.1 on a W2K machine.

And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
(Terry Pratchett, Small Gods)

Replies are listed 'Best First'.
Re: How to catch Net::FTP timeouts ?
by steves (Curate) on Sep 24, 2004 at 12:10 UTC

    You can catch anything using eval blocks:

    eval {$ftp->get("that.file")}; if ($@ =~ /Timeout/) { # catching code goes here }
    Net::FTP also lets you set the timeout value, so you could try increasing it if that's the problem.

      You can catch anything using eval blocks:

      Absolutes are seldom absolute....

      eval { exit }; print "I caught it\n" if $@;

      Cheers - L~R

Re: How to catch Net::FTP timeouts ?
by cosimo (Hermit) on Sep 24, 2004 at 12:18 UTC
    If I understand well, what you want is explained in the documentation. Here it is an extract from Net::FTP docs:
    CONSTRUCTOR new (HOST [,OPTIONS]) This is the constructor for a new Net::FTP object. "HOST" is the name of the remote host to which a FTP connection is required. "OPTIONS" are passed in a hash like fashion, using key and value pairs. Possible options are: Firewall - The name of a machine which acts as a FTP ... Port - The port number to connect to on the remote machine for the FTP connection Timeout - Set a timeout value (defaults to 120) Debug - debug level (see the debug method in the Net::Cmd manpage) ...
    Timeout here is the key option, so you can say:
    my $ftp = Net::FTP->new( $myhost, Timeout => 10, Debug => 1 ); ... $ftp->get($myfile) or warn("Can't get file $myfile"); $ftp->quit();
    Hope this helps.
      Thank you for the hint, but we had the default, which is 2 minutes.
      For a quasi inhouse line, there should be no timeouts at all, but this problem is not Perl related ;-) If it does not recover after 120s it will never come back.

      And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
      (Terry Pratchett, Small Gods)

Re: How to catch Net::FTP timeouts ?
by zakzebrowski (Curate) on Sep 24, 2004 at 13:44 UTC
    Actually, the perldoc synopsis for Net::FTP says:
    $ftp->get("that.file") or die "get failed ", $ftp->message;
    which would solve your problem...

    Zak - the office
      This only works, if the file does not exist, but as soon as the transfer starts, it returns the filename, even if the file is only downloaded partially. You can restart the download with the proper parameters, but you have to find out first.

      The eval version seems to be the answer to my problem.

      Thanks again for all help.

      And it came to pass that in time the Great God Om spake unto Brutha, the Chosen One: "Psst!"
      (Terry Pratchett, Small Gods)

        Gratias. I'll keep that in mind.

        Zak - the office
Re: How to catch Net::FTP timeouts ?
by djberg96 (Acolyte) on Sep 25, 2004 at 01:23 UTC
    If you're interested in a "If at first you don't succeed, sleep a while and try again later" approach, consider

    Available on CPAN. :)

    - Dan

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://393466]
Approved by Happy-the-monk
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-08-09 21:44 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (55 votes). Check out past polls.