我想在这个perl脚本中使用代理,但我不确定如何使用代理。
#!/usr/bin/perl
use IO::Socket;
$remote = IO::Socket::INET->new(
Proto => "tcp",
PeerAddr => "localhost",
PeerPort => "8080",
)
or die "cannot connect";
print $remote "GET / HTTP/1.0\n\n";
while ( <$remote> ) { print }
答案 0 :(得分:9)
使用LWP :: UserAgent模块,该模块内置proxy support。
答案 1 :(得分:8)
use LWP::UserAgent;
$ua = LWP::UserAgent->new;
$ENV{HTTP_proxy} = "http://ip:port";
$ua->env_proxy; # initialize from environment variables
my $req = HTTP::Request->new(GET => 'http://google.com/');
print $ua->request($req)->as_string;
delete $ENV{HTTP_PROXY};
答案 2 :(得分:1)
直接来自我的一个剧本:
use LWP::UserAgent;
my($ua) = LWP::UserAgent->new;
if ($opts->{'proxy'}) {
my($ip) = Sys::HostIP->hostip;
if (($ip =~ m{^16\.143\.}) ||
($ip =~ m{^161\.}) ||
($ip =~ m{^164\.})) {
$ua->proxy(http => 'http://localhost:8080');
}
else {
$ua->proxy(http => "");
}
}
else {
$ua->env_proxy;
}
#***** get current entry *****
my($req) = HTTP::Request->new(GET => "http://stackoverflow.com/questions/1746614/use-proxy-with-perl-script");
my($raw) = $ua->request($req)->content;