http://qs1969.pair.com?node_id=401734

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

Esteemed monks,

I am working on Win32, using Net::FTP to download a file. I would like to pass progress information to a progress bar, how to do it is easy - I have done it for other things, but is Net::FTP non-blocking? Is there a pure-perl method that is? How do I find the total download file size and the 'downloaded thus far' values?

jdtoronto

Replies are listed 'Best First'.
Re: FTP with progress bar in Tk?
by tilly (Archbishop) on Oct 23, 2004 at 01:51 UTC
    I know that this is the possible with the LWP library. Install libwww-perl and look at its documentation. For a concrete example of a program that keeps interactive track of progress while it is downloading, you can study lwp-download.
      Thanks tilly,

      For some reason I think of LWP as a browser substitute, I have used it for HTTP requests a number of times. I even have the Perl & LWP book here. I have looked at the code for lwp-download and I think I need to go.

      jdtoronto

Re: FTP with progress bar in Tk?
by zentara (Archbishop) on Oct 23, 2004 at 14:13 UTC
    You can also use wget or libcurl in a thread or a piped open,and get progress out of it. With wget it's a bit harder because you have to filter it's output, and do regexes to get the percentage done. I just posted a method on comp.lang.perl.tk. Do a search at http://groups.google.com for "non-blocking downloads with indicator" and you will get one you can modify.

    Or libcurl has an easy way to get download progress. Here is an example and you can fork-exec to run it, or use a thread to run it non-blocking and get your percentage back to your tk app. I just posted a way to do this here->Tk-with-worker-threads You can put the code below into the worker thread code, and pass a url to it through the shared hash

    #!/usr/bin/perl use warnings; use strict; use WWW::Curl::easy; # Read URL to get my $url = "http://zentara.zentara.net/~zentara/cgi-bin/avi-out.cgi"; # Init the curl session my $curl = WWW::Curl::easy->new(); if ($curl == 0) {print "not ok $!\n "} $curl->setopt(CURLOPT_NOPROGRESS, 0); $curl->setopt(CURLOPT_FOLLOWLOCATION, 1); sub prog_callb{ my ($clientp,$dltotal,$dlnow,$ultotal,$ulnow)=@_; my $percentage = ($dlnow/$dltotal)*100; print STDERR "dltotal: $dltotal, dlnow: $dlnow\t", sprintf("%.0f",$pe +rcentage),"% complete\n"; return 0; } $curl->setopt(CURLOPT_PROGRESSFUNCTION, \&prog_callb); open HEAD, ">$0-head.out"; $curl->setopt(CURLOPT_WRITEHEADER, *HEAD); open BODY, ">$0-body.out"; $curl->setopt(CURLOPT_FILE,*BODY); $curl->setopt(CURLOPT_URL, $url); # Add some additional headers to the http-request: #my @myheaders; #$myheaders[0] = "Server: www"; #$myheaders[1] = "User-Agent: Perl interface for libcURL"; #$curl->setopt(CURLOPT_HTTPHEADER, \@myheaders); # Go get it my $retcode=$curl->perform(); if ($retcode == 0) { my $bytes = $curl->getinfo(CURLINFO_SIZE_DOWNLOAD); print STDERR "$bytes bytes read "; my $realurl=$curl->getinfo(CURLINFO_EFFECTIVE_URL); my $httpcode=$curl->getinfo(CURLINFO_HTTP_CODE); print STDERR "effective fetched url (http code: $httpcode) was: $u +rl "; } else { # We can acces the error message in $errbuf here print STDERR "$retcode / ".$curl->errbuf."\n"; } exit;

    I'm not really a human, but I play one on earth. flash japh
      zentara

      Thanks for the info, but I am working in Win32 (principally, aslthough my code does run on Linux no-obne seems to want it there!) so I need to either useseomnthing antive there, or a perl module.

      jdoronto

Re: FTP with progress bar in Tk?
by tachyon (Chancellor) on Oct 23, 2004 at 12:55 UTC

    You will find an LWP example in this chapter of the O'reily Spidering Hacks book. With Net::FTP you can get the size easily using the size() method. There is a hash method that prints hashes every X bytes so the hook you need is kinda there, the problem is that the only way to get at it is to hack the source/or override the get() method in Net::FTP.pm with a modified routine that has a suitable callback hook. Either that or (speculates) pass the get method a tied file handle that lets you tail the writing to it by Net::FTP.

    cheers

    tachyon

      Thanks tachyon

      This is actually turning into yet another great learning exercise. I have the Spidering book and I will study it carefully. I thiknk your code can be very easilly adapted for what I need.

      jdtoronto