develooper Front page | perl.libwww | Postings from August 2003

Re: Downloading a part of a file from a remote server

Thread Previous | Thread Next
From:
Octavian Rasnita
Date:
August 7, 2003 11:59
Subject:
Re: Downloading a part of a file from a remote server
Message ID:
005b01c35d16$505b72e0$946de9d5@microsoft.com
Thank you for this code. It works beautifully.

I have a few more questions regarding this subject.
1. Please tell me how to use the header information 'Range' =>
sprintf("bytes=%s-%s"
if I want to use the other style for getting the content of a file like:

my $request = HTTP::Request->new('GET', $url, ...);
my $response = $ua->request($request);
my $content = $response->content();

2. The second question is if the entire content of a file will be downloaded
from the server and trimmed locally, or the program will download only the
selected part.

3. Why do I need to use the sprintf function to write bytes=300-500? Is it
any problem if I just write the $start and $end variables?

4. Please tell me what is this line doing:
for(${ $request->content_ref }) { # trim end, then beginning:

I don't know what content_ref  could be.

Thank you very much.

PS. I found a solution for my first question in using a line like this:
my $request = HTTP::Request::Common::GET($url, 'Range' =>
'bytes=${start}-${end}');

I hope that I will be able to set and get cookies to $request and $response
if I will be using this method.

Teddy,
Teddy's Center: http://teddy.fcc.ro/
Email: orasnita@home.ro

----- Original Message -----
From: "Sean M. Burke" <sburke@cpan.org>
To: "Octavian Rasnita" <orasnita@home.ro>; <libwww@perl.org>
Sent: Monday, May 26, 2003 6:06 AM
Subject: Re: Downloading a part of a file from a remote server


At 09:34 PM 2003-05-25 +0300, Octavian Rasnita wrote:
>Is the LWP module able to download only a certain part of a file from a
>remote location, lets say from the character 3000 up to the character
>5000? I want to create a kind of download manager and I would like to use
LWP.

use strict;
use LWP;
my $browser = LWP::UserAgent->new;

my($start, $end) = (300,500);
my $url = 'http://www.suck.com/daily/2000/06/19/daily.html';


my $r = $browser->get( $url,
   'Range' => sprintf( "bytes=%s-%s", $start, $end),
);
die "Failed: " . $r->headers_as_string
  unless $r->is_success;

unless( length($r->content) == ($end - $start + 1) ) {
   print "Server didn't support partial ranges. Trimming.\n";
   # Or try dying!

   for(${ $r->content_ref }) { # trim end, then beginning:
     printf "%s => %s   in  %s\n", $start, $end, length($_);
     substr($_,$end+1,  ) = '' unless   $end >= length($_);
     substr($_,0, $start) = '' unless $start >  length($_);
     printf "%s => %s   in  %s\n", $start, $end, length($_);
   }
}

print "[\n", $r->content, "\n]\n";

--
Sean M. Burke    http://search.cpan.org/~sburke/






Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About