Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to download a range of bytes?

by Zeokat (Novice)
on Dec 26, 2007 at 17:56 UTC ( #659125=perlquestion: print w/replies, xml ) Need Help??

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

Well... im 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 dont know how to setup it into the code to do my objective, im very noob with perl and im 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 18: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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://659125]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2019-09-16 08:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (186 votes). Check out past polls.

    Notices?