我有一个脚本应该能够对具有不同IP地址的服务进行一些调用。我的代码工作时,我没有设置任何IP到我的电话,我写了一个函数调用做之前分配一个IP对象,但它返回一个错误:为什么我不能为LWP :: UserAgent分配一个IP?
Can't locate object method "local_address" via package "LWP::UserAgent"
这是我的功能结构:
#!/usr/bin/perl -w
use LWP::UserAgent;
use HTTP::Headers;
use HTTP::Request::Common;
use HTTP::Cookies;
use URI::Escape;
use HTML::LinkExtor;
# set user agent object values
my $ua = new LWP::UserAgent;
$ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.10) Gecko/20050716 Firefox/1.0.6');
push @{ $ua->requests_redirectable }, 'POST';
$ua->cookie_jar({});
sub set_caller_ip {
my($set_ip_address) = @_;
$ua->local_address("$set_ip_address");
return 1;
}
sub test_caller_ip {
my $req = new HTTP::Request('GET', 'http://whatismyip.org/');
$req->headers->push_header('Connection','Keep-Alive');
$req->headers->push_header('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
my $res = $ua->request($req) or die "$!";
return $res->content();
}
这是我给他们打电话的方式:
set_caller_ip($caller_ip_address);
$caller_ip_tested = test_caller_ip();
print "\$caller_ip_tested=".$caller_ip_tested."\n";die;
你知道有什么问题?
感谢您的帮助提前!
感谢您的提示!这真的可能!让我检查版本! – Monica 2012-03-02 23:26:27
你是绝对正确的......谢谢你!我在这个问题上挣扎了3个小时......而实际上我不是系统管理员,所以现在我应该学习如何升级这个模块!无论如何...感谢您的帮助:) – Monica 2012-03-02 23:31:32
@Monica,当你看到'无法找到对象方法'消息时,基本上有3个选项:你拼错方法名称,你有错误的对象类型,或者该模块的版本与您正在阅读的文档(以及API已更改)不匹配。 – cjm 2012-03-02 23:34:48