All pastes #476573 Raw Edit

Ploximeth

public text v1 · immutable
·published 2007-05-08 05:30 UTC
rendered paste body
#!/usr/bin/perl -w 
my $eval = "use threads\;";
eval($eval);
die "$0: please rebuild your perl with threads\n" if ($@);
use threads::shared;
use strict;           # Keep me clean 
use IO::Socket::INET; # Sockets would be nice
use Net::HTTP;

my $version = 1.1;

my $tmp;
my @raepurl;
my @threads;
my $urlstoraep = 0;
my $failed   : shared = 0;
my $bytesdl  : shared = 0;
my $succeded : shared = 0;

sub raeper($);

for (;$tmp=shift;) {
	if ($tmp eq "-h" || $tmp eq "--help") {
		print "$0: halraep program version $version\n";
		print "parameters: $0 <url to raep> <another url> <another one..> ... <n'th>\n";
	}
	else {
		push(@raepurl, $tmp);
		$urlstoraep++;
	}
}

print "No URL's specified to raep.\n" if $urlstoraep < 1;
die("\n") if $urlstoraep < 1;
for ($tmp = 0; $tmp < $urlstoraep; $tmp++) {
	my $handle = threads->new(\&raeper, $raepurl[$tmp]);
	push(@threads, $handle);
	$handle->detach;
}

while (1337) { # no nada
	
}

sub raeper($) {
	my $self  = threads->self();
	my $tid   = $self->tid();
	my $url   = shift;
	$url      =~ s/http\:\/\///g;
	my $time  = localtime(time());
	my $host  = (split(/\//, $url))[0];
	my $toget = "/".substr($url, length($host)+1, 8192);
	my $tmp;
	my $bytes;
	my $fail;
	my $n;
	my $buf;
	print "[thr$tid:$time] starting raep on $url\n";
	while (1) {
        	my $s = Net::HTTP->new(Host => $host);
		if (!$s) {
			$failed++;
			print "[thr$tid:".localtime(time())."] failed to get $url\n";
		}
		$s->write_request(GET => $toget, 'User-Agent' => "plasmaraepr $version");
		my($code, $mess, %h) = $s->read_response_headers;
		while ($s->read_entity_body($buf, 1024)) {
			$bytes += length($buf);
		}
		$bytesdl += $bytes;
		$succeded++;
		print "[thr$tid:".localtime(time())."] ".conv($bytes)."\t$toget\t\t".conv($bytesdl)."\t\t$failed:$succeded\n";
		$bytes = 0;	
	}
}

sub conv {
	my $bytes = shift;
	return $bytes."b" if $bytes < 1024;
	return rtwo($bytes/1024)."KiB" if ($bytes > 1024 && $bytes < (1024**2));
	return rtwo($bytes/(1024**2))."MiB" if ($bytes > (1024**2) && $bytes < (1024**3));
	return rtwo($bytes/(1024**3))."GiB" if ($bytes > (1024**3) && $bytes < (1024**4));
	return rtwo($bytes/(1024**4))."TiB" if ($bytes > (1024**4) && $bytes < (1024**5));
}

sub rtwo { my $n=shift;return sprintf("%.2f",$n); }