返回列表 回复 发帖

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

作者:小歪歪
email: annysun@163.net
日期:2000-07-10 15:22:43
  1. #!/usr/bin/perl
  2. use IO::Socket;
  3. if (&#36;&#35;ARGV <= 0) {
  4.   print STDERR "usage: getright <URL> <FILENAME>\n\n";
  5.   print STDERR "     <URL>: eg. http://www.server.dom:port/path/file.ext\n";
  6.   print STDERR "<FILENAME>: eg. filename.ext\n";
  7.   exit(0);
  8. } else {
  9.   open(FILE, "+>>".&#36;ARGV[1]) or die "Cannot open &#36;ARGV[1] for append: &#36;!";
  10.   (&#36;length = sysseek(FILE,0,2)) =~ s!.*([0-9]+).*!&#36;1!g;
  11.   print STDERR "Attempting to resume &#36;ARGV[1] from byte: &#36;length\n";
  12. }
  13. if (&#36;ARGV[0] =~ m!^ (?:http://)? (.*?) (?:\:([0-9]+))? (/.*)&#36;!x)
  14.   { (&#36;server,&#36;port,&#36;path) = (&#36;1, &#36;2 || 80, &#36;3); }
  15. print "[&#36;server] [&#36;port] [&#36;path]\n";
  16. &#36;socket = IO::Socket::INET->new(PeerAddr => &#36;server,
  17.                                 PeerPort => &#36;port,
  18.                                 Proto    => ';tcp';,
  19.                                 Type     => SOCK_STREAM) or die "Cannot connect: &#36;!";
  20. print &#36;socket "GET &#36;path HTTP/1.0\n";
  21. print &#36;socket "Host: &#36;server\n";
  22. print &#36;socket "Range: bytes=&#36;length-\n";
  23. print &#36;socket "Connection: close\n\n";
  24. if (!((&#36;reply = <&#36;socket>) =~ /HTTP\/1.[01] 206 Partial Content/)) {
  25.   &#36;reply =~ s!(.*)\r\n!&#36;1!;
  26.   print STDERR "Failed [&#36;reply]\n";
  27.   print STDERR "Invalid URL/Unrecognized Reply/Resume Not Supported.\n";
  28.   close(&#36;socket); exit(0);
  29. } else {
  30.   print STDERR "Received valid HTTP reply.\n";
  31. }
  32. while ((&#36;mime = <&#36;socket>) =~ /\w+: /) {
  33.   if (&#36;mime =~ /Content\-Range\:\sbytes\s([0-9]+)\-([0-9]+)\/([0-9]+)/)
  34.     { (&#36;start,&#36;finish,&#36;filesize) = (&#36;1, &#36;2, &#36;3); }
  35.   if (&#36;mime =~ /Content\-Length\:\s([0-9]+)/) { &#36;total = &#36;1; }
  36. }
  37. print STDERR "Receiving data: ";
  38. while (&#36;data = <&#36;socket>) {
  39.   &#36;recieved += length(&#36;data);
  40.   &#36;percentage= int(((&#36;start+&#36;recieved) / &#36;filesize) * 100);
  41.   print STDERR &#36;percentage."%"."\b"x(length(&#36;percentage)+1);
  42.   print FILE &#36;data;
  43. }
  44. print STDERR "100%\n";
  45. close(FILE);
  46. close(&#36;socket);
  47. &#35; Example HTTP return header:
  48. &#35;
  49. &#35;          HTTP/1.1 206 Partial content
  50. &#35;          Date: Wed, 15 Nov 1995 06:25:24 GMT
  51. &#35;          Last-modified: Wed, 15 Nov 1995 04:58:08 GMT
  52. &#35;          Content-Range: bytes 21010-47021/47022
  53. &#35;          Content-Length: 26012
  54. &#35;          Content-Type: image/gif
复制代码
——————以下内容由Cnangel2005年08月25日 08:01pm时添加———————

    非常好!不说什么了

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表