Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Reporting Progress With Net::FTP (was : Net::FTP question)

by smoss (Acolyte)
on Apr 25, 2002 at 11:08 UTC ( [id://161917]=perlquestion: print w/replies, xml ) Need Help??

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

I am running a script that downloads multiple files via Net::FTP, I now want to use the output from FTP that you normally see when manually doing an FTP get i.e. X amount of bytes in X amount of seconds for getting throughput stats. How can I gather this information and enter it into an array or string? Is this possible? Thanks.

edited by boo_radley : Title change

Replies are listed 'Best First'.
Re: Net::FTP question
by Dog and Pony (Priest) on Apr 25, 2002 at 11:28 UTC
    Well, of course, but you will have to do this yourself. Easiest way to do this implementationwise would be simply to time the call(s) to get etc. and stat the file(s) transfered for bytes. Then you can output this, or store it in any way you choose.

    A bit trickier perhaps, but maybe more extensible etc in the long run might be to look into the dataconn class that Net::FTP uses, and look into subclassing/using that one, and the more low-level calls. Note that this will mean more work to make a simple get/put, but on the other hand, the source code is there so you can see how it Net::FTP does it, and adapt.


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.
Re: Net::FTP question
by mattr (Curate) on Apr 25, 2002 at 11:31 UTC
    Net::FTP::Common seems to let you do this by capturing STDERR. Otherwise with Net::FTP you might spend a lot of time fiddling with a dataconn object.

    Or, you could calculate elapsed time and divide into the file size (use a file test) yourself.. seems STDERR messages would differ according to the server. The timing could be done pretty accurately if you use Time::HiRes.

Re: Net::FTP question
by suaveant (Parson) on Apr 25, 2002 at 13:51 UTC
    If you want to do this as the download is happening you can use the filehandle method of Net::FTP...
    $| = 1; # autoflush, because we are using \b to overwrite current line my $time = time(); my $FILE = $FTP->stor($filename); my ($buf,$bytes); while(my $bytecount = $FILE->read($buf, 10024)) { $bytes += $bytecount; print "\b",($bytes/(time()-$time+.01),"b/s"; # .01 to prevent divide + by zero }
    This is by no means tested, and, could probably be tweaked much better, but it gives the idea.

    I not sure but maybe Benchmark's timeit function would be a good way to get back the time the $FTP->get takes, as well... rather than grabbing POSIX::times... that is if you want better than a second granularity :)

                    - Ant
                    - Some of my best work - (1 2 3)

Re: Net::FTP question
by atopolc (Pilgrim) on Apr 25, 2002 at 14:13 UTC
    Net::FTP supports the size command so you can get the bytes just from calling size but this only works if remote host supports it so you will probably want to check to see if size is supported. If size is not supported then you would probably want to roll your own at that point. Here is how I do it..
    if ($self->supported(size)) { my $ret=$self->size($file); print "\t$ret bytes transferred\n"; } else { #see earlier example }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found