BBS水木清华站∶精华区
发信人: brill (清清小河蠢蠢笨鱼), 信区: Linux
标 题: plget 0.1
发信站: BBS 水木清华站 (Sat Jun 26 00:27:12 1999)
因为我遇到的那个wget的问题,做了这么个小东西。
也不是自己做的,拿别人的东西改了改,呵呵
功能非常简单,就是给个URL然后就死命的往回拽:))
不过只支持HTTP。就因为简单,可以随心所欲的改啦...
源码如下:
#!/usr/bin/perl
# Base on a program named "getright" from AKA homepage.
# Modified by brill@smth. 06/25/1999
use IO::Socket;
if ($#ARGV < 0) {
print STDERR "usage: plget <URL>\n\n";
exit(0);
} else {
$ARGV[0] =~ m!/([^/]*)$!;
$filename = $1;
open(FILE, "+>>".$filename) or die "Cannot open $filename for append: $!";
$length = sysseek(FILE,0,2);
}
if ($ARGV[0] =~ m!^ (?:http://)? (.*?) (?:\:([0-9]+))? (/.*)$!x)
{ ($server,$port,$path) = ($1, $2 || 80, $3); } else { die "error URL\n"; }
print "[$server] [$port] [$path] -> [$filename]\n";
if ($length > 0)
{ print STDERR "Attempting to resume $filename from byte: $length\n"; }
REGET:
print STDERR "Connecting...";
$socket = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp',
Type => SOCK_STREAM) or die "Cannot connect: $!";
print STDERR "Connected...";
print $socket "GET $path HTTP/1.0\n";
print $socket "Host: $server\n";
if ($length > 0)
{ print $socket "Range: bytes=$length-\n"; }
print $socket "Connection: close\n\n";
$reply = <$socket>;
$reply =~ s!(.*)\r\n!$1!;
print STDERR "Replay: [$reply]\n";
if ($length > 0)
{
if (!(($reply) =~ /HTTP\/1.[01] 206 Partial Content/)) {
print STDERR "Invalid URL/Unrecognized Reply/Resume Not Supported.\n";
close($socket); exit(0);
}
}
else
{
if (!(($reply) =~ /HTTP\/1.[01] 200 OK/)) {
print STDERR "Invalid URL/Unrecognized Reply.\n";
close($socket); exit(0);
}
}
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; }
}
if (! $filesize) { $filesize = $total; }
print STDERR "Receiving data of ", $filesize, ": ";
while ($data = <$socket>) {
$length += length($data);
$percentage= int(($length / $filesize) * 100);
print STDERR $percentage."%"."\b"x(length($percentage)+1);
print FILE $data;
}
if ($length < $filesize)
{
print STDERR "Break. Retry...\n";
goto REGET;
}
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
--
※ 修改:·brill 於 Jun 26 09:09:50 修改本文·[FROM: 166.111.68.179]
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.68.179]
BBS水木清华站∶精华区