【发布时间】:2015-04-09 08:55:59
【问题描述】:
我需要在一次 Perl 运行(进程)中更改代理服务器。但是LWP::UserAgent 总是记住 first 代理值。
当我使用代理x.x.x.2 启动我的程序时,此地址始终用作代理。
怎么了?
简单程序示例:
getHTTP('http://my.com/', "http://x.x.x.1:3128");
getHTTP('http://my.com/', "http://x.x.x.2:3128");
getHTTP('http://my.com/', "");
getHTTP('http://my.com/', "http://x.x.x.3:3128");
sub getHTTP {
my ($url, $proxy) = @_;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->proxy(http => $proxy);
my $req = HTTP::Request->new(GET => $url);
return $ua->request($req)->as_string;
}
HTTP 日志的输出:
x.x.x.1 - - [09/Feb/2015:15:28:06 +0100] "GET / HTTP/1.0" 200 3467
x.x.x.1 - - [09/Feb/2015:15:29:07 +0100] "GET / HTTP/1.0" 200 3467
y.y.y.y - - [09/Feb/2015:15:29:08 +0100] "GET / HTTP/1.0" 200 3467
x.x.x.1 - - [09/Feb/2015:15:29:09 +0100] "GET / HTTP/1.0" 200 3467
(y.y.y.y 是我的无代理地址)
操作系统:Debian GNU/Linux 7
新发现: 当我运行下一个 perl 或 shell 脚本时。结果和上面一样。
proxy.sh:
curl -x "http://x.x.x.1:3128" http://my.com/
curl -x "http://x.x.x.2:3128" http://my.com/
curl -x "http://x.x.x.3:3128" http://my.com/
结果:总是 x.x.x.1 代理
proxy.pl:
`curl -x "http://x.x.x.4:3128" http://my.com/`;
`curl -x "http://x.x.x.5:3128" http://my.com/`;
`curl -x "http://x.x.x.6:3128" http://my.com/`;
结果:总是 x.x.x.4 代理
代理设置似乎无法在一个 shell 进程中更改。
【问题讨论】:
-
这看起来不错。它与您正在运行的实际代码不同吗?
-
(我的原始代码要复杂得多。)但是当我运行这个简单的例子时,输出是一样的。换句话说,这个简单的程序会导致所描述的行为。
-
会不会是代理配置的原因?代理是否以某种方式连接以进行负载平衡,或者它们可以选择使用哪个接口?
-
我不这么认为。请查看我的新发现!
标签: perl shell proxy terminal lwp-useragent