Hi all,
I'm using LWP::Parallel and it works good for me. But I have one curious problem - I want change PUA agent for every request. But I don't know how to to that. Result of this code are 15 txt files, but, content is not what I expect. I'd like to have in TXT something like this:
"HTTP_HOST: 2ge.mine.nu" in every file
"HTTP_USER_AGENT: My agent x" where x is number 0-15
Just run this piece of code and you will see.
use strict;
use warnings;
use LWP::Parallel::UserAgent;
my $http = 'http://2ge.mine.nu/headers.php';
my $max_hosts = 10;
#registration of PARALLEL USER AGENT
my $pua = LWP::Parallel::UserAgent->new();
$pua->in_order (0); # handle requests in order
$pua->duplicates(0); # ignore duplicates
$pua->timeout (10); # in seconds
$pua->redirect (0); # follow redirects
$pua->max_hosts ($max_hosts); # connect in parallel server
$pua->max_req ($max_hosts); # max req per host
my $counter = 0;
my @agents = map "My agent $_", 1..15;
for (my $i=0; $counter<@agents; $i++) {
my @reqs_l = ();
for (my $j=0; $j<$max_hosts; $j++) {
push( @reqs_l,HTTP::Request->new('GET', $http) );
}
my $reqs = \@reqs_l; #this could be better?
foreach my $req (@$reqs) {
if (defined $agents[$counter]) {
$pua->agent("$agents[$counter]");
print "Registering agent: ".$counter."\n";
if ( my $res = $pua->register ($req) ) {
print STDERR $res->error_as_HTML;
}
}
$counter++;
}
my $entries = $pua->wait();
my $cnt = 0;
foreach (keys %$entries) {
print "WRITING content to $cnt.txt\n";
open(OUT,">$cnt.txt");
my $res = $entries->{$_}->response;
print OUT $res->content( );
close OUT;
$cnt++;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.