星星博客's Archiver

cnangel 发表于 2005-8-25 20:00

一个支持HTTP续传下载的PERL程序

[color=red]作者:小歪歪
email: [email]annysun@163.net[/email]
日期:2000-07-10 15:22:43[/color]
[code]
#!/usr/bin/perl
use IO::Socket;
if (&#36;&#35;ARGV <= 0) {
  print STDERR "usage: getright <URL> <FILENAME>\n\n";
  print STDERR "     <URL>: eg. http://www.server.dom:port/path/file.ext\n";
  print STDERR "<FILENAME>: eg. filename.ext\n";
  exit(0);
} else {
  open(FILE, "+>>".&#36;ARGV[1]) or die "Cannot open &#36;ARGV[1] for append: &#36;!";
  (&#36;length = sysseek(FILE,0,2)) =~ s!.*([0-9]+).*!&#36;1!g;
  print STDERR "Attempting to resume &#36;ARGV[1] from byte: &#36;length\n";
}
if (&#36;ARGV[0] =~ m!^ (?:http://)? (.*?) (?:\:([0-9]+))? (/.*)&#36;!x)
  { (&#36;server,&#36;port,&#36;path) = (&#36;1, &#36;2 || 80, &#36;3); }
print "[&#36;server] [&#36;port] [&#36;path]\n";
&#36;socket = IO::Socket::INET->new(PeerAddr => &#36;server,
                                PeerPort => &#36;port,
                                Proto    => ';tcp';,
                                Type     => SOCK_STREAM) or die "Cannot connect: &#36;!";
print &#36;socket "GET &#36;path HTTP/1.0\n";
print &#36;socket "Host: &#36;server\n";
print &#36;socket "Range: bytes=&#36;length-\n";
print &#36;socket "Connection: close\n\n";
if (!((&#36;reply = <&#36;socket>) =~ /HTTP\/1.[01] 206 Partial Content/)) {
  &#36;reply =~ s!(.*)\r\n!&#36;1!;
  print STDERR "Failed [&#36;reply]\n";
  print STDERR "Invalid URL/Unrecognized Reply/Resume Not Supported.\n";
  close(&#36;socket); exit(0);
} else {
  print STDERR "Received valid HTTP reply.\n";
}
while ((&#36;mime = <&#36;socket>) =~ /\w+: /) {
  if (&#36;mime =~ /Content\-Range\:\sbytes\s([0-9]+)\-([0-9]+)\/([0-9]+)/)
    { (&#36;start,&#36;finish,&#36;filesize) = (&#36;1, &#36;2, &#36;3); }
  if (&#36;mime =~ /Content\-Length\:\s([0-9]+)/) { &#36;total = &#36;1; }
}
print STDERR "Receiving data: ";
while (&#36;data = <&#36;socket>) {
  &#36;recieved += length(&#36;data);
  &#36;percentage= int(((&#36;start+&#36;recieved) / &#36;filesize) * 100);
  print STDERR &#36;percentage."%"."\b"x(length(&#36;percentage)+1);
  print FILE &#36;data;
}
print STDERR "100%\n";
close(FILE);
close(&#36;socket);
&#35; Example HTTP return header:
&#35;
&#35;          HTTP/1.1 206 Partial content
&#35;          Date: Wed, 15 Nov 1995 06:25:24 GMT
&#35;          Last-modified: Wed, 15 Nov 1995 04:58:08 GMT
&#35;          Content-Range: bytes 21010-47021/47022
&#35;          Content-Length: 26012
&#35;          Content-Type: image/gif
[/code]

[color=#CC0000][b]——————以下内容由[u]Cnangel[/u]在[u]2005年08月25日 08:01pm[/u]时添加———————[/b][/color]

    非常好!不说什么了

页: [1]

Powered by Discuz! Archiver 7.0.0  © 2001-2009 Comsenz Inc.