Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Net::FTP problems

by panaman (Acolyte)
on Dec 29, 2003 at 11:40 UTC ( [id://317408]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I am writing a seismology remote seismograph station event data retrieval program. Big and long but trivial really. I use net::ftp. My problem is that occasionally one of the FTP's to one of the stations the transfer hangs up. (Lousy connections here in Panama) I get one of two results. One is that program just sits there and the other is that the net::ftp module times out sends a time out error message and tosses me out to the operating system. Both outcomes are lousy.

I need to recover from a failed FTP transfer in an orderly way. for instance:

$test = $ftp->get($remote_file,$local_file);

Works fine if the transfer was ok or if the transfer was damaged. In one case it return the name of the file retrieved and in the other returns false.

If the transfer hangs nothing happens and if it times out in one of the net::ftp pm's it tosses me out into the OS.

Am I using Net::FTP wrong?

Is there a way to trap the Net::FTP error message before find myself in the operating system so that I can act on it.

Is there a way to make my own timeout function?

Any other suggestions?

I have read the Net::ftp docs and understand them the best I can.

> Happy New year,

Angel Rodriguez, headline@volcanbaru.com

Replies are listed 'Best First'.
Re: Net::FTP problems
by rob_au (Abbot) on Dec 29, 2003 at 12:21 UTC
    Is there a way to trap the Net::FTP error message before find myself in the operating system so that I can act on it?

    For this you can use the eval function to trap fatal errors from Net::FTP functions.

    Is there a way to make my own timeout function?

    For this you will want to have a read through the alarm function and the perlipc documentation.

    For example:

    my $retrieval = undef; my $timeout = 120; eval { local $SIG{'ALRM'} = sub { die 'timeout' }; alarm $timeout; $retrieval = $ftp->get($remote_file, $local_file); alarm 0; }; if ($@) { if ($@ =~ /timeout/) { } }

     

    perl -le "print+unpack'N',pack'B32','00000000000000000000001010100001'"

      Thank for your good answer, it helps. the "eval" seems to have stopped the being tossed into the OS. I am going to move this app over to a linux box since I have discovered that windows does not do signal proccessing acording to the perldocs. thanks
Re: Net::FTP problems
by matsmats (Monk) on Dec 29, 2003 at 13:41 UTC

    I know what you feel, the same problems with Net::FTP was bothering me for quite a while. What solved it for me was checking all values in use - and wrapping the whole process in an eval.

    The code below is a result of fixing things that has gone wrong over time, and now it doesn't crash my program anymore. It uses the Net::FTP default timeout of 10 minutes (if memory serves me right), which doesn't make the program die, and has evals and checks on the things that do make it die:

    if ($ftp_address && $ftp_user && $ftp_password) {
        eval {  
            my $ftp = Net::FTP->new($ftp_address, Passive => 1);
            if ($ftp) {
                $ftp->login($ftp_user,$ftp_password);
                $ftp->cwd($ftp_directory) if $ftp_directory;
                $ftp->put($ftp_filename);
                $ftp->quit;
            } else  {
                # FTP object no good
            }
        }; # Eval ends.
       
        warn "error $@" if $@;
    }
    
      Hi, Thank for your good answer. The "eval" seems to have stopped the being tossed into the OS. thanks
Re: Net::FTP problems
by nimdokk (Vicar) on Dec 29, 2003 at 12:59 UTC
    You might also consider adding an or die to the line as well. Something like:
    $test = $ftp->get($remote_file,$local_file) or die "Could not get $rem +ote_file. $!";
    That in addition to the other suggestions.

    Hope that helps a bit :-)


    "Ex libris un peut de tout"
Re: Net::FTP problems
by diskcrash (Hermit) on Dec 30, 2003 at 01:22 UTC
    Panaman-

    This is a great app for Perl . When I had to do EDI transfers with Net:FTP even on a decent LAN/WAN there would be service interruptions and hangs. It was more effective to check to see if the other system was there first by using Net::Ping or now Net::Ping::External , with a timeout. This avoided 99% of bad FTP tries. You just wait a few minutes and retry the Ping if it fails.

    Buena Suerte,

    Diskcrash

Re: Net::FTP problems
by Anonymous Monk on Dec 29, 2003 at 22:28 UTC
    wget is sometimes a good solution. stable and proofed. mUrat

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found