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

Well... i´m using LWP module to download a txt file, but needs to download only the latest 1000 bytes, i read that it can be possible using Range Bytes http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 But i don´t know how to setup it into the code to do my objective, i´m very noob with perl and i´m searching for help. Thanks in advance. Merry christmass.
#!/usr/bin/perl -w use strict; use LWP::UserAgent; use HTTP::Request; my $ua = LWP::UserAgent->new; $ua->agent("Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"); my $timeout = 30; my $url = 'http://localhost/log.txt'; print "Downloading log: " ,$url , "\n"; my $req = HTTP::Request->new(GET => $url); #$req->header(¿?¿?¿?¿?¿?); my $response = $ua->request($req); my $content = $response->content(); open SOURCE, '>>', 'log.txt' or die $!; print SOURCE $content;

Replies are listed 'Best First'.
Re: How to download a range of bytes?
by eserte (Deacon) on Dec 26, 2007 at 23:27 UTC
    This seems to work:
    #!/usr/bin/perl -w use strict; use LWP::UserAgent; my $ua = LWP::UserAgent->new; my $url = 'http://localhost/...'; $ua->default_headers->push_header(Range => "bytes=1000-2000"); my $response = $ua->get($url); my $content = $response->content(); warn length($content); warn $content;
    To get the current content length of the object, you can do a HEAD before and look at the content-length header.
      The code works verrrrrrry good eserte. Big thanks. But new question arrive to my head, are there any way to know if the server have the abbility of "Accept-Ranges: bytes" ?? Thanks in advance.
        Try fetching with HEAD instead of GET to view the Accept* headers without getting the content itself