用来检查库里的代理列表是否依旧可用
多线程是用上了,但是不知道缺省是起几个线程
#!/usr/bin/perl
use strict;
use DBI();
use HTTP::ProxyCheck;
use threads;
my $DEBUG = 1;
# Connect to the database.
my $TimeToLive = 5; # 3 days
my $dbh = DBI->connect("DBI:mysql:database=proxyservers;host=localhost;mysql_socket=/var/lib/mysql/mysql.sock",
"root", "", {’RaiseError’ => 1});
my $sql = "select id, ipn, port from proxy where now() - INTERVAL 1 day > t_checked order by t_checked";
print $sql if ($DEBUG);
my $sth = $dbh->prepare("$sql");
$sth->execute();
while (my $ref=$sth->fetchrow_hashref()) {
my $thr = threads->new(\&CheckHTTPProxy, "$ref->{’ipn’}", "$ref->{’port’}");
if ($thr->join) {
# if (CheckHTTPProxy($ref->{’ipn’}, $ref->{’port’})) {
$sql = "update proxy set t_checked = now() where id = $ref->{’id’}";
print $sql if ($DEBUG);
$dbh->do("$sql");
}
}
$sql = "delete from proxy where now() - INTERVAL $TimeToLive day > t_checked";
print $sql if ($DEBUG);
$dbh->do("$sql");
$dbh->disconnect();
sub CheckHTTPProxy
{
my ($ipn, $port) = @_;
my $ip = num2str($ipn);
my $proxy = "$ip:$port";
my $url = ‘http://search.cpan.org/’;
my $proxy_check = new HTTP::ProxyCheck(
proxy => $proxy,
url => $url,
answer_size => ‘header’,
print_error => 0,
) or return 0;
print "Trying to connect to ‘$proxy’ and retrieve ‘$url’\n" if ($DEBUG);
if ( $proxy_check->check() ) {
print "’$proxy’ returns:\n\n", $proxy_check->get_answer(), "\n\n" if ($DEBUG);
return 1;
} else {
print "Error: ", $proxy_check->get_error(), "\n" if ($DEBUG);
return 0;
}
}
sub num2str
{
my ($ipn) = @_;
my $z = $ipn % 256;
$ipn >>= 8;
my $y = $ipn % 256;
$ipn >>= 8;
my $x = $ipn % 256;
$ipn >>= 8;
my $w = $ipn % 256;
return "$w.$x.$y.$z";
}
Tags: Tags: Perl 作者 哈哈
No Comments »