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

I know that when I start a new connection with an ftp server, I can put a timeout limit on it. Is there a way to put a time limit on the other methods, such as "get" or "put"?

Thanks, John Bobinyec

Replies are listed 'Best First'.
Re: How to FTP with Timeout?
by ww (Archbishop) on Nov 10, 2011 at 20:05 UTC
    And the Perl part of your question is ...?

      Okay. With

      use Net::FTP; $ftp = Net::FTP->new("some.host.name", Debug => 0); $ftp->login("anonymous",'-anonymous@'); $ftp->cwd("/pub"); $ftp->get("that.file"); $ftp->quit;

      Is there any way to put a time limit on the actual file transfer?

      Thanks again.

      John Bobinyec

        Try the option TimeOut in the constructor. By default this option will take as 120 check the link "http://perldoc.perl.org/Net/FTP.html"

        use Net::FTP; $ftp = Net::FTP->new("some.host.name", Debug => 0, Timeout => 300); $ftp->login("anonymous",'-anonymous@'); $ftp->cwd("/pub"); $ftp->get("that.file"); $ftp->quit;

        This is untested but you could try to put an eval-alarm combination in there to time the get. Something like:
        #!/usr/bin/perl use warnings; use strict; use Net::FTP; $ftp = Net::FTP->new("some.host.name", Debug => 0); $ftp->login("anonymous",'-anonymous@'); my $timeout = 60; my $time; eval { local $SIG{ALRM} = sub { die "timeout!" }; alarm $timeout; $ftp->cwd("/pub"); $ftp->get("that.file"); $ftp->quit; $time = alarm 0; } if ($@) { if ( $@ =~ /timeout/ ) { #do something } else { #do something else for other failures } } else { print "External call finished in " . ( $timeout - $time ) . " seco +nds\n"; }

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh