A timeout question using Net::FTP

Discussion in 'Perl Misc' started by Trudge, Jul 28, 2012.

  1. Trudge

    Trudge Guest

    I have a script to download files. It checks the remote and local directories, and downloads any new files from the remote location.

    Here is part of my code:
    my $ftp = Net::FTP->new
    (
    "ftp.xxx.yy",
    Timeout => 3600,
    Debug => 0
    ) or die "Could not connect: $@\n";

    My question is, what units are the Timeout option in? The docs don't make it clear, and I'm guessing the units are seconds. Anyone have a definitive answer?
    --
    Trudge, Jul 28, 2012
    #1
    1. Advertising

  2. Trudge

    Trudge Guest

    On Saturday, July 28, 2012 7:23:56 AM UTC-4, Ben Morrow wrote:
    > Quoth Trudge:
    >
    > > I have a script to download files. It checks the remote and local

    >
    > > directories, and downloads any new files from the remote location.

    >
    > >

    >
    > > Here is part of my code:

    >
    > > my $ftp = Net::FTP->new

    >
    > > (

    >
    > > "ftp.xxx.yy",

    >
    > > Timeout => 3600,

    >
    > > Debug => 0

    >
    > > ) or die "Could not connect: $@\n";

    >
    > >

    >
    > > My question is, what units are the Timeout option in? The docs don't

    >
    > > make it clear, and I'm guessing the units are seconds. Anyone have a

    >
    > > definitive answer?

    >
    >
    >
    > The timeout is passed to IO::Select, so it's in seconds, possibly
    >
    > fractional.
    >
    >
    >
    > Ben


    Thank you Ben (again) for clearing something up for myself and possibly others. That gives me a starting point.
    --
    Amer Neely
    Trudge, Jul 28, 2012
    #2
    1. Advertising

  3. Trudge

    J. Gleixner Guest

    On 08/05/12 18:04, Cal Dershowitz wrote:
    > On 07/28/2012 05:23 AM, Ben Morrow wrote:
    >>
    >> Quoth Trudge<>:
    >>> I have a script to download files. It checks the remote and local
    >>> directories, and downloads any new files from the remote location.
    >>>
    >>> Here is part of my code:
    >>> my $ftp = Net::FTP->new
    >>> (
    >>> "ftp.xxx.yy",
    >>> Timeout => 3600,
    >>> Debug => 0
    >>> ) or die "Could not connect: $@\n";
    >>>
    >>> My question is, what units are the Timeout option in? The docs don't
    >>> make it clear, and I'm guessing the units are seconds. Anyone have a
    >>> definitive answer?

    >>
    >> The timeout is passed to IO::Select, so it's in seconds, possibly
    >> fractional.

    >
    > Idunno, Ben:
    >
    >
    > $ perltidy safe_post11.pl

    Why do we need to see this??

    > $ perl safe_post11.pl my_ftp

    You can make safe_post11.pl an executable so you don't have to type
    'perl' every time.

    > ..........
    > success is 22
    > $ cat safe_post11.pl
    > #!/usr/bin/perl -w
    > use strict;
    > use 5.010;
    > use Net::FTP;
    > use diagnostics;
    >
    > $| = 1; # Disable output buffering
    >
    > ## usage: perl safe_post11.pl server_name
    > my $ident = 'my_ftp.txt';
    > my ($config, $domain);
    > $config = do($ident);
    > unless ($config) {
    > die("read error: $!") if $!;
    > die("parse error: $@") if $@;
    > }
    >
    > $domain = $config->{$ARGV[0]};
    > die("unknown domain: $ARGV[0]") unless $domain;
    >
    > # printf("server\t%s\nuser\t%s\npass\t%s\n",
    > # $domain->{domain},
    > # $domain->{username},
    > # $domain->{password});


    We don't need to see most of the noise above. Show only the
    specific code that describes your problem.

    >
    > my $ftp = Net::FTP->new( $domain->{domain}, Timeout => 5 ) or die "Can't
    > connect: $@\n";


    > $ftp->login( $domain->{username}, $domain->{password} )
    > or die "Couldn't login\n";

    Always include the reason/message. When it fails it might tell you why.
    >
    > for (1..10) {
    > print '.';
    > sleep 1;
    > }
    > print "\n";
    >
    > my @r = $ftp->ls();
    > my $success = scalar(@r);
    > print "success is $success\n";

    That seems like an odd way to determine success.
    >
    > # close $ftp;
    > $
    >
    > cpan says 120 is the default, but haven't I changed that?


    Yes. You don't show the failure that is taking longer than 5
    seconds to timeout though.

    Using sleep there doesn't do anything to show there's a problem. The
    login() will wait for the connection to succeed or fail. The timeout
    is how long to wait around for a response, it doesn't have anything to
    do with how long it's connected.

    If you want to do something with the connection based on elapsed
    time, see perldoc -f alarm . Usually that's not needed because
    it usually connects and you do what's needed and close the connection,
    or you can't connect or the operation times out and you handle the error.
    J. Gleixner, Aug 6, 2012
    #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. Nico Grubert
    Replies:
    0
    Views:
    617
    Nico Grubert
    Nov 24, 2005
  2. Mark Probert

    Timeout::timeout and Socket timeout

    Mark Probert, Oct 6, 2004, in forum: Ruby
    Replies:
    1
    Views:
    1,268
    Brian Candler
    Oct 6, 2004
  3. Jeff Miller
    Replies:
    0
    Views:
    463
    Jeff Miller
    Mar 26, 2009
  4. D. Buck
    Replies:
    2
    Views:
    459
    D. Buck
    Jun 29, 2004
  5. Asaf Sinai
    Replies:
    1
    Views:
    231
    Chris Davies
    Jul 4, 2006
Loading...

Share This Page