Net::FTP Wierdness; or Not.....

H

huntingseasonson

Doing batches of FTP transfers. Most of the time they work, sometimes
they dont. When they dont, the error messages seem to be the response
from the FTP server acknowledging my previous command, or empty.

Heres the code:
sub ftp_transfer()
{
my $ftp = Net::FTP->new($host) || _error("Can't connect to host $host:
$@");


_netcmd_error($ftp,"Can't login to $host as $username: "
$ftp->message)
unless $ftp->login($username,$password);


if( $ftp->ls($package) ) {

_log("Remote directory $package already exists");

unless($config{overwrite}) { #no need to continue unless
caller wants to overwrite
$ftp->quit;
return;
}

_log("Overwriting remote directory $package");
}
else {
_netcmd_error($ftp,"Can't create remote directory $package: " .
$ftp->message)
unless $ftp->mkdir($package);
}

_netcmd_error($ftp,"Can't change to remote directory $package: "
.. $ftp->message)
unless $ftp->cwd($package);
_netcmd_error($ftp,"Can't transfer file $file: " . $ftp->message)
unless $ftp->put($file);

_netcmd_error($ftp,"Can't switch to binary mode: " .
$ftp->message) unless $ftp->binary;

###############################################################################
#ERROR COMES FROM HERE
_netcmd_error($ftp,"Can't transfer file $video: " . $ftp->message)
unless $ftp->put($video);
################################################################################

$ftp->quit;

return 1;
}

_error() logs the error and sends an email alert, _netcmd_error calls
quit() on the ftp object and then calls _error()

The error I get is either: "Opening BINARY mode data connection for
xyz.mpg"
or empty.

Humm, I guess its probably better to call binary() 1st. But I cant
understand why the 2nd put() call fails with either nothing in
message(), or the response from the server when I called binary(). The
perldocs say that all calls return a true or false to indicate success
or failure, unless otherwise noted. So it is failing, but the message
is faulty. I really dont know.

Thanks
 
B

Ben Morrow

Quoth (e-mail address removed):
Doing batches of FTP transfers.

It is considered polite here to write in complete sentences.
Most of the time they work, sometimes
they dont. When they dont, the error messages seem to be the response
from the FTP server acknowledging my previous command, or empty.

Heres the code:
sub ftp_transfer()
^^
Do you know what this does? If not, don't put it there.
{
my $ftp = Net::FTP->new($host) || _error("Can't connect to host $host:
$@");

What is $host? I hope you're not passing values into subroutines through
globals: that's considered very bad style, for a number of good reasons.
_netcmd_error($ftp,"Can't login to $host as $username: "
$ftp->message)

This line is not valid Perl. Are you missing a .?
unless $ftp->login($username,$password);

This would be much clearer written as

$ftp->login($username, $password)
or _netcmd_error $ftp,
"Can't login to $host as $username: " . $ftp->message;

The point of the and and or operators is that they let you put the
important bit--what you're doing--first, and the error-handling
afterwards.
if( $ftp->ls($package) ) {

_log("Remote directory $package already exists");

unless($config{overwrite}) { #no need to continue unless
caller wants to overwrite
$ftp->quit;
return;
}

_log("Overwriting remote directory $package");
}
else {

unless (...) { } else { } is very confusing. If you need both branches,
just use an ordinary if.
_netcmd_error($ftp,"Can't create remote directory $package: " .
$ftp->message)
unless $ftp->mkdir($package);
}

_netcmd_error($ftp,"Can't change to remote directory $package: "
. $ftp->message)
unless $ftp->cwd($package);
_netcmd_error($ftp,"Can't transfer file $file: " . $ftp->message)
unless $ftp->put($file);

_netcmd_error($ftp,"Can't switch to binary mode: " .
$ftp->message) unless $ftp->binary;

#################################################
#ERROR COMES FROM HERE
_netcmd_error($ftp,"Can't transfer file $video: " . $ftp->message)
unless $ftp->put($video);
#################################################

$ftp->quit;

return 1;
}

_error() logs the error and sends an email alert, _netcmd_error calls
quit() on the ftp object and then calls _error()

The error I get is either: "Opening BINARY mode data connection for
xyz.mpg"
or empty.

Humm, I guess its probably better to call binary() 1st. But I cant
understand why the 2nd put() call fails with either nothing in
message(), or the response from the server when I called binary(). The
perldocs say that all calls return a true or false to indicate success
or failure, unless otherwise noted. So it is failing, but the message
is faulty. I really dont know.

From a quick look at the source of Net::FTP, I would say that if the put
fails sometime during the transfer (that is, if the STOR FTP command
succeeds but the actual transfer fails) then $ftp->message will not
contain anything useful. Try your transfer passing the Debug => 1 option
to Net::FTP to see if you can work out what's going wrong.

Ben
 
H

huntingseasonson

Quoth (e-mail address removed):



Do you know what this does? If not, don't put it there.

sure, type checking arguments, in this case, we accept none
globals: that's considered very bad style, for a number of good reasons.
indeed



$ftp->login($username, $password)
or _netcmd_error $ftp,
"Can't login to $host as $username: " . $ftp->message;
From a quick look at the source of Net::FTP, I would say that if the put
fails sometime during the transfer (that is, if the STOR FTP command
succeeds but the actual transfer fails) then $ftp->message will not
contain anything useful. Try your transfer passing the Debug => 1 option
to Net::FTP to see if you can work out what's going wrong.

Ben

I will try debug
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top