我正在尝试使用Perl发送多个HTTP get请求。我需要使用sock代理发送这些请求。
如果我使用下面的代码,我可以发送一个带有sock代理的请求
#!/usr/bin/perl
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(
agent => q{Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; YPC 3.2.0; .NET CLR 1.1.4322)},
);
$ua->proxy([qw/ http https /] => 'socks://localhost:9050'); # Tor proxy
#$ua->cookie_jar({});
$a = 10;
while ( $a < 20 ) {
my $rsp = $ua->get('http://example.com/type?parameter=1¶meter=2');
print $rsp->content;
$a = $a + 1;
}它工作成功,但我需要使用AnyEvent并行发送多个GET请求
#!/usr/bin/perl
use strict;
use AnyEvent;
use AnyEvent::HTTP;
use Time::HiRes qw(time);
use LWP::Protocol::socks;
use AnyEvent::Socket;
my $cv = AnyEvent->condvar( cb => sub {
warn "done";
});
my $urls = [
"http://url-1-withallparameters",
"http://url-2-withallparameters",
"http://url-3-withallparameters",
];
my $start = time;
my $result;
$cv->begin(sub { shift->send($result) });
for my $url ( @$urls ) {
$cv->begin;
my $now = time;
my $request;
$request = http_request(
GET => $url,
timeout => 2, # seconds
sub {
my ($body, $hdr) = @_;
if ($hdr->{Status} =~ /^2/) {
push (@$result,
join("\t",
($url,
" has length ",
$hdr->{'content-length'},
" and loaded in ",
time - $now,
"ms"))
);
}
else {
push (@$result,
join("",
"Error for ",
$url,
": (",
$hdr->{Status},
") ",
$hdr->{Reason})
);
}
undef $request;
$cv->end;
}
);
}
$cv->end;
warn "End of loop\n";
my $foo = $cv->recv;
print join("\n", @$foo), "\n" if defined $foo;
print "Total elapsed time: ", time-$start, "ms\n";这工作得很好,但我不能使用sock代理发送这些请求。
即使在终端中,如果我导出像curl和wget这样的代理命令,它们也可以很好地使用sock代理,但是当我使用Perl命令作为sock代理执行这个脚本时,它不能工作。
我可以将它与LWP::UserAgent集成,但它不能与AnyEvent一起工作。
我用过
proxy => [ $host, $port ],下面
GET => $url, 但它只适用于HTTP和HTTPS代理,不适用于sock代理。此url适用于HTTP/HTTPS代理,但不适用于sock代理。
发布于 2018-05-12 00:02:03
documentation for AnyEvent::HTTP有这样的功能
AnyEvent::HTTP不直接支持
Socks代理
如果您阅读了该部分,它可能描述了适合您的解决方法
或者,看看AnyEvent::HTTP::Socks,上面写着
此模块将新的‘socks’选项添加到由AnyEvent::HTTP导出的所有http_*函数。因此您可以为HTTP请求指定socks代理。
或者AnyEvent::HTTP::LWP::UserAgent,它应该允许您使用已有的工作LWP::UserAgent代码中的想法
https://stackoverflow.com/questions/50295787
复制相似问题