Re: Download file over HTTP
by l3nz (Friar) on Nov 25, 2003 at 10:04 UTC
|
Frankly, I don't see much of a problem with LWP, simply use the content method and not as_string.
The piece of code belows downloads a file and compares it with itself using the SHA algorithm in order to prove the download was correct. I tested it with a number of binary files that were available on my Windows box and always worked right.
use strict;
use LWP::UserAgent;
use Digest::SHA1;
my $srcfile = "c:/...../cab/WBCust.CAB";
my $tempdl = "temp.tmp";
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(GET => 'http://127.0.0.1/...../cab/WBCust
+.CAB');
my $r = $ua->request($req)->content;
open F, ">$tempdl" or die "$! $tempdl";
binmode F;
print F $r;
close F;
computeSHA( $srcfile );
computeSHA( $tempdl );
unlink $tempdl;
exit;
sub computeSHA() {
my ($srcfile) = @_;
my $sh_pre = Digest::SHA1->new;
open S1, $srcfile or die "$! $srcfile";
$sh_pre->addfile(*S1);
close S1;
print "$srcfile:\n " . $sh_pre->hexdigest . "\n";
}
The result is something like:
c:/...../cab/WBCust.CAB:
3816f34e861b6268c716b9f06091dee0d580f2cb
temp.tmp:
3816f34e861b6268c716b9f06091dee0d580f2cb
| [reply] [d/l] [select] |
|
|
Thanks everyone for you help, you've all given me very useful advice!
Thanks
Chris
| [reply] |
|
|
OK it looks like I'll be downloading a 45MB file, is LWP ok/the right choice to be downloading this file?
Thanks
Chris
| [reply] |
|
|
What if it drops the connection halfway through on a slower link? You will have to cater for that, like resuming a download at a given position, but not all servers allow that.
I'd say it's best to yourself a download manager, like getright, netant, etc, that are specialized in downloading large files efficiently from the Internet. They can do special speed optimization too, like openning multiple connections and download sections of the file simutaneously.
| [reply] |
|
|
Re: Download file over HTTP
by davis (Vicar) on Nov 25, 2003 at 09:57 UTC
|
$response = $ua->get('http://search.cpan.org/',
':content_file' => '/tmp/sco.html'
);
Cheers
davis
It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
| [reply] [d/l] |
Re: Download file over HTTP
by Abigail-II (Bishop) on Nov 25, 2003 at 08:54 UTC
|
use WWW::Mechanize;
Abigail | [reply] [d/l] |
|
|
How would that help save the file?
| [reply] |
|
|
Abigail-II gave a good advice to use the WWW::Mechanize module from CPAN. WWW::Mechanize does allow downloading/saving of a file easily (as with LWP::UserAgent).
The WWW::Mechanize module was derived from the LWP::UserAgent module, which has a mirror function to save/download a file:
$ua->mirror( $url, $filename )
If you go to the examples of the WWW::Mechanize module, you will find an example program from merlyn that 'walks' a site and 'suck down' all the images, which would apply to files in this case.
| [reply] [d/l] |
A reply falls below the community's threshold of quality. You may see it by logging in.
|
Re: Download file over HTTP
by Anonymous Monk on Nov 25, 2003 at 10:03 UTC
|
I have got the logging in part done with the code shown below, but how do I download the file and save it? The example I show is more for downloading a web page. Anyone have any ideas?
Downloading a webpage via http is the same as downloading a zip file via http (no difference).
Instead of print $ua->request($req)->as_string,
open a filehandle and print to it (or just redirect the output of your script to a file).
| [reply] [d/l] |
Re: Download file over HTTP
by iburrell (Chaplain) on Nov 25, 2003 at 20:45 UTC
|
You set the username and password for basic authentication on the user agent.
$ua->credentials($host, $realm, $user, $pass);
The mirror method downloads the file without storing the response in memory. Using content on the response puts the whole file in memory which can be a problem with big files.
$ua->mirror($url, $file);
| [reply] [d/l] [select] |