kazak has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to add a handler for LWPUserAgent, this handler must to choose UserAgent string for each request and choose and to choose a proxy for this request, but something is going wrong, makerequest subroutine isn't applied to requests. Thanks in advance.

Script:

my (@agents,@routes); open( AGENTS, "<", "/etc/agents.cfg" ); while( <AGENTS> ) { s/#.*//; next if /^(\s)*$/; chomp; push @agents, $_; } close(AGENTS); open( ROUTES, "<", "/etc/routes.cfg" ); while( <ROUTES> ) { s/#.*//; next if /^(\s)*$/; chomp; push @routes, $_; } close(ROUTES); my $ua = LWP::UserAgent->new(); sub cb { my($request, $ua, $h) = @_; my $i = $#agents + 1; $i = rand($i); $i = int $i; my $j = $#routes + 1; $j = rand($j); $j = int $j; my $cur_agent = $agents[$i]; my $cur_proxy = $routes[$j]; $request->header($cur_agent); $ua->timeout(10); $ua->proxy(['http'],$cur_proxy); } $ua->add_handler( request_prepare => \&cb); ....

Replies are listed 'Best First'.
Re: LWP add_handler issue
by Anonymous Monk on Jan 17, 2012 at 00:28 UTC