您的位置: 首页-> 电脑文摘-> 网站开发-> CGI/PERL-> 正文

一个支持HTTP续传下载的PERL程序
作者佚名 来源InterNet 加入时间:2005-3-31
#!/usr/bin/perl
use IO::Socket;

if ($#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, "+>>".$ARGV[1]) or die "Cannot open $ARGV[1] for append: $!";
($length = sysseek(FILE,0,2)) =~ s!.*([0-9]+).*!$1!g;
print STDERR "Attempting to resume $ARGV[1] from byte: $length\n";
}

if ($ARGV[0] =~ m!^ (?:http://)? (.*?) (?:\:([0-9]+))? (/.*)$!x)
{ ($server,$port,$path) = ($1, $2 || 80, $3); }

print "[$server] [$port] [$path]\n";

$socket = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
Type => SOCK_STREAM) or die "Cannot connect: $!";

print $socket "GET $path HTTP/1.0\n";
print $socket "Host: $server\n";
print $socket "Range: bytes=$length-\n";
print $socket "Connection: close\n\n";

if (!(($reply = <$socket>) =~ /HTTP\/1.[01] 206 Partial Content/)) {
$reply =~ s!(.*)\r\n!$1!;
print STDERR "Failed [$reply]\n";
print STDERR "Invalid URL/Unrecognized Reply/Resume Not Supported.\n";
close($socket); exit(0);
} else {
print STDERR "Received valid HTTP reply.\n";
}

while (($mime = <$socket>) =~ /\w+: /) {
if ($mime =~ /Content\-Range\:\sbytes\s([0-9]+)\-([0-9]+)\/([0-9]+)/)
{ ($start,$finish,$filesize) = ($1, $2, $3); }
if ($mime =~ /Content\-Length\:\s([0-9]+)/) { $total = $1; }
}

print STDERR "Receiving data: ";
while ($data = <$socket>) {
$recieved += length($data);
$percentage= int((($start+$recieved) / $filesize) * 100);
print STDERR $percentage."%"."\b"x(length($percentage)+1);
print FILE $data;
}

print STDERR "100%\n";
close(FILE);
close($socket);

# Example HTTP return header:
#
# HTTP/1.1 206 Partial content
# Date: Wed, 15 Nov 1995 06:25:24 GMT
# Last-modified: Wed, 15 Nov 1995 04:58:08 GMT
# Content-Range: bytes 21010-47021/47022
# Content-Length: 26012
# Content-Type: image/gif



[文章录入员:nancy]

相关文章
  • 用Perl制作页面计数器
  • 用Perl语句来代替常用的操作系统命令
  • 用 perl 实现文件上传
  • perl 域名查询程序
  • Perl的经典用法
  • Perl 程序的属性祥解
  • perl中的日期处理
  • perl在win32平台上直接操作打印机
  • Perl用于实现遗传算法
  • Perl 字符匹配实例解说
  • 相关软件:
  • PERL 编程24学时教程
  • Perl Template Toolkit
  • Perl 24 小时自学通
  • CGI-Perl实例起步
  • ::PCBOOKCN'ADS::


    ::CGI/PERL::
    ASP/ASP.NET
    PHP技术
    JSP技术
    XML技术
    CGI/PERL
    WEB服务器
    其它
    ::阅读排行::
    ·perl中的日期处理
    ·深入研究表单提交方式:GET/P...
    ·如何利用Perl开发Intern...
    ·本地调试PERL CGI程序
    ·CGI应用程序开发基础
    ·PerlScript编写ASP
    ·PERL与MySQL(DBI接口...
    ·Perl用于实现遗传算法
    ·CGI编程--Perl的安装配置
    ·ASP、CGI、ISAPI、OD...