How can I check that I downloaded the entire file in Perl?
Does the test of the file-size make sense here? I tried to cut the connection while downloading, but it looks like the size-test is never reached.
#!/usr/bin/env perl
use warnings;
use strict;
use 5.012;
use LWP::Simple;
my $url = 'http://www.kernel.org/pub/linux/kernel/v2.6/next/patch-v2.6.34-rc5-next-20100428.bz2';
my $file = 'next_kernel.bz';
my $fi开发者_StackOverflow社区le_size = '3462166';
my $response = getstore( $url, $file );
if ( is_success $response ) {
if ( $file_size == -s $file ) {
say "OK";
}
else {
say "Not OK";
}
}
If you just want to exercise the test code, put in the wrong file size and assert that your download is not "OK". If you want to test what happens with an aborted connection, you need a much larger file -- a 2342-byte download should only take a fraction of a fraction of a second.
This is weird, I got the same results as you. I don't have a solution, but I report here a bit of hacking around your problem.
What is happening is that the file saved is bigger than expected and there was no connection break. Trying to bunzip2 it will give error, even after truncating the resulting file at desired size.
Exact same result was achieved by using LWP::UserAgent with :content_file parameter to get() method. Same again adding a response_data handler to $ua object.
But the following command got me good results:
GET http://www.kernel.org/pub/linux/kernel/v2.6/next/patch-v2.6.34-rc5-next-20100428.bz2 > next_kernel.bz
And since GET is the lwp-request script, based on LWP, you might find answers by checking its code if you're willing to dig that deep.
Hope this helps you.
I suppose it doesn't hurt, but I'm fairly certain is_success
will not return true if the connection is aborted.
精彩评论