• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 20:04 | 作者: admin | 来源:  网友评论 | 查看: 15次 | 进入软件测试论坛讨论

领测软件测试网 #!/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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网