Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

[Resolved] LWP add_handler issue

by kazak (Beadle)
on Jan 16, 2012 at 22:35 UTC ( [id://948225]=perlquestion: print w/replies, xml ) Need Help??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://948225]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found