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

Discussion in 'Perl Misc' started by huntingseasonson@gmail.com, Nov 10, 2006.

  1. Guest

    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
     
    , Nov 10, 2006
    #1
    1. Advertising

  2. Ben Morrow Guest

    Quoth :
    > 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

    --
    I touch the fire and it freezes me, []
    I look into it and it's black.
    Why can't I feel? My skin should crack and peel---
    I want the fire back... Buffy, 'Once More With Feeling'
     
    Ben Morrow, Nov 10, 2006
    #2
    1. Advertising

  3. Guest

    On Nov 9, 7:10 pm, Ben Morrow <> wrote:
    > Quoth :
    >
    > > 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.


    sure, type checking arguments, in this case, we accept none

    > > {
    > > 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.


    indeed

    > > _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;
    >
    > > #ERROR COMES FROM HERE
    > > _netcmd_error($ftp,"Can't transfer file $video: " . $ftp->message)
    > > unless $ftp->put($video);
    > > #################################################

    >
    > > $ftp->quit;

    >
    > > return 1;
    > > }


    > > 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


    I will try debug
     
    , Nov 11, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Larry
    Replies:
    0
    Views:
    400
    Larry
    Feb 17, 2006
  2. Nico Grubert
    Replies:
    0
    Views:
    657
    Nico Grubert
    Nov 24, 2005
  3. Nico Grubert
    Replies:
    0
    Views:
    461
    Nico Grubert
    Nov 25, 2005
  4. D. Buck
    Replies:
    2
    Views:
    528
    D. Buck
    Jun 29, 2004
  5. Replies:
    3
    Views:
    208
    John Mason Jr
    Dec 18, 2004
Loading...

Share This Page