How to check if the downloaded file is loaded using the getstore () function?

I wrote a quick script to upload files using LWP :: Simple library and its getstore () function. It works pretty well, but sometimes the downloaded file is not complete. I do not know what this causes, but when I load it manually, wget in the command line file is fine.

I would suggest that the damaged files are caused by a connection being deleted or something similar, although I run my script on a dedicated line in a data center connection, somewhere between my server and the remote server, a crash may occur.

This is my code:

sub download {
my $status = getstore($_[0], $_[1]);
if (is_success($status)) { return 1; } else { return 0; }
}

What are the possible solutions to this problem? How to check if the transfer has passed, and if the file is completed and not damaged?

Thanks for your valuable answers.

+3
3

$status, , LWP:: . , , .

. MD5 SHA, . , , , .

+2

is_success() true HTTP 2XX, , , , "206 Partial Content", .

, 200 , .

+3

:

use LWP;
use HTTP::Request::Common;
my $ua = LWP::UserAgent->new;
$ua->timeout(3);
my $res = $ua->request(HEAD $url); # just to get headers of a file
my $length_full = $res->headers->{'content-length'};
...
$res = $request(GET $url);
my $length_got = $res->content_length;
if ($length_got != $length_full) { print "File have not been downloaded completely!\n";
...
+3

Source: https://habr.com/ru/post/1759648/


All Articles