Timeout value in Net::FTP seems to have no effect

H

Henry Law

I expect I'm doing something wrong, but Googling for [perl "Net::FTP"
timeout problem] shows nothing; nor does perldoc -q ftp. Can someone
point me in the right direction?

(ActiveState Perl, "v5.8.7 built for MSWin32-x86-multi-thread").

I use Net::FTP to pass files around my internal network. Occasionally I
specify a host that isn't up, at which point Net::FTP hangs for a long
time before giving up. "Set a timeout," I thought, but using the
Timeout=>nnn option on the login operation seems to have no effect -
though the documented default of 120 seconds isn't in operation either.
Here's a sample program which should work with any server that
supports anonymous FTP.

------------------------- sample -----------------------------
#! /usr/bin/perl

use strict; use warnings;
use Net::FTP;

my ($host,$timeout) = @ARGV;
exit 0 unless defined $host;
$timeout = 120 unless defined $timeout;

my $start_time = time;

print "Logging in to $host with timeout $timeout seconds\n";
my $ftp = Net::FTP->new( $host, Timeout=>$timeout );
unless (defined $ftp) {
print "Timed out in ",(time - $start_time)," seconds\n";
print "Session to '$host' failed\n";
exit 0;
}

$ftp->login( 'anonymous', '(e-mail address removed)' );
my $cwd = $ftp->pwd();
print "Current directory at $host is '$cwd'\n";
$ftp->quit();
print "Session finished\n";
-------------------------- sample output follows -----------------
F:\User\PC\NFBack>tryit.pl neptune 5
Logging in to neptune with timeout 5 seconds
Current directory at neptune is '/'
Session finished

F:\User\PC\NFBack>tryit.pl uranus 5
Logging in to uranus with timeout 5 seconds
Timed out in 21 seconds
Session to 'uranus' failed

F:\User\PC\NFBack>tryit.pl uranus 1000
Logging in to uranus with timeout 1000 seconds
Timed out in 21 seconds
Session to 'uranus' failed
----------------------------- end ---------------------------------
 
L

l v

Henry said:
I expect I'm doing something wrong, but Googling for [perl "Net::FTP"
timeout problem] shows nothing; nor does perldoc -q ftp. Can someone
point me in the right direction?

(ActiveState Perl, "v5.8.7 built for MSWin32-x86-multi-thread").

I use Net::FTP to pass files around my internal network. Occasionally I
specify a host that isn't up, at which point Net::FTP hangs for a long
time before giving up. "Set a timeout," I thought, but using the
Timeout=>nnn option on the login operation seems to have no effect -
though the documented default of 120 seconds isn't in operation either.


Because Timeout is the amount of time given to each ftp statement in
your script, not the login.
F:\User\PC\NFBack>tryit.pl neptune 5
Logging in to neptune with timeout 5 seconds
Current directory at neptune is '/'
Session finished

F:\User\PC\NFBack>tryit.pl uranus 5
Logging in to uranus with timeout 5 seconds
Timed out in 21 seconds
Session to 'uranus' failed

F:\User\PC\NFBack>tryit.pl uranus 1000
Logging in to uranus with timeout 1000 seconds
Timed out in 21 seconds
Session to 'uranus' failed
----------------------------- end ---------------------------------

In your test example, it is taking 21 seconds for your computer where
the script is running to *not* find uranus. I suspect your hanging ftp
session is caused by a different scenario. Perhaps you can test for
server availability prior to the ftp login using a different
approach/module. Socket.pm comes to mind. Open a connection to uranus
on port 21. If connects, then you are free to Net::FTP.

Len
 
H

Henry Law

Because Timeout is the amount of time given to each ftp statement in
your script, not the login.

Uh huh; that fits into the category "doing something wrong". I knew it
.... thanks.
Perhaps you can test for
server availability prior to the ftp login using a different
approach/module. Socket.pm comes to mind. Open a connection to uranus
on port 21. If connects, then you are free to Net::FTP.

Makes sense. SMOP.
 
T

Tintin

l v said:
In your test example, it is taking 21 seconds for your computer where
the script is running to *not* find uranus.

A script that can find uranus? I'd like to see that?

Open a connection to uranus
on port 21. If connects, then you are free to Net::FTP.

Sounds medically impossible to me ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top