1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#!/usr/bin/perl
use warnings;
use strict;
# POE::Component::Client::HTTP uses HTTP::Request and response
# objects.
use HTTP::Request::Common qw(GET);
use HTTP::Cookies;
my $cookie = HTTP::Cookies->new(
file => "lwp_cookies.dat",
autosave => 1,
);
# A list of pages to fetch. They will be fetched in parallel. Add
# more sites to see it in action.
my @url_list =
qw( <a href="http://poe.perl.org/misc/test.html" target="_blank">http://poe.perl.org/misc/test.html</a>
);
# Include POE and the HTTP client component.
use POE qw(Component::Client::HTTP);
# Create a user agent. It will be referred to as "ua". It limits
# fetch sizes to 4KB (for testing). If a connection has not occurred
# after 180 seconds, it gives up.
POE::Component::Client::HTTP->spawn
( Alias => 'coucoubot',
MaxSize => 4096, # Remove for unlimited page sizes.
Timeout => 180,
CookieJar => $cookie,
FollowRedirects => 2
);
# From
# Create a session for each request.
foreach my $url (@url_list) {
POE::Session->create
( inline_states =>
{ _start => sub {
my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
# Post a request to the HTTP user agent component. When the
# component has an answer (positive or negative), it will
# send back a "got_response" event with an HTTP::Response
# object.
$kernel->post( ua => request => got_response => GET $url );
},
# A response has arrived. Display it.
got_response => sub {
my ( $heap, $request_packet, $response_packet ) = @_[ HEAP, ARG0, ARG1 ];
# The original HTTP::Request object. If several requests
# were made, this can help match the response back to its
# request.
my $http_request = $request_packet->[0];
# The HTTP::Response object.
my $http_response = $response_packet->[0];
# Make the response presentable, and display it.
my $response_string = $http_response->as_string();
$response_string =~ s/^/| /mg;
print ",", '-' x 78, "\n";
print $response_string;
print "`", '-' x 78, "\n";
},
},
);
}
# Run everything, and exit when it's all done.
$poe_kernel->run();
exit 0; |
Partager