Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: Asynchronous HTTP Request using specific ports

by zwon (Monsignor)
on Apr 04, 2013 at 14:08 UTC ( #1026993=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Asynchronous HTTP Request using specific ports
in thread Asynchronous HTTP Request using specific ports

If I try it asynchronous way it still works:

use 5.010; use strict; use warnings; use AnyEvent; use AnyEvent::Handle; use IO::Socket::INET; my $sock1 = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 7777, LocalPort => 6666, ReuseAddr => 1, ) or die $!; my $ah1; $ah1 = AnyEvent::Handle->new( fh => $sock1, on_read => \&send_echo ); my $sock2 = IO::Socket::INET->new( PeerAddr => '127.0.0.1', PeerPort => 7778, LocalPort => 6666, ReuseAddr => 1, ) or die $!; my $ah2; $ah2 = AnyEvent::Handle->new( fh => $sock2, on_read => \&send_echo ); AE::cv->recv; sub send_echo { my $handle = shift; $handle->push_write( substr $handle->{rbuf}, 0, length $handle->{r +buf}, '' ); }

PS: perhaps you should show us example that doesn't work for you


Comment on Re^3: Asynchronous HTTP Request using specific ports
Download Code
Re^4: Asynchronous HTTP Request using specific ports
by Adamba (Sexton) on Apr 04, 2013 at 15:10 UTC

    That doesn't seems to work:

    #!/usr/bin/perl use strict; use warnings; use AnyEvent; use AnyEvent::HTTP; use AnyEvent::Socket qw( tcp_connect ); use Socket; open my $fh, "<", "host_list.txt"; my @hosts = <$fh>; close $fh; chomp @hosts; my @ports = qw/65011 65021 65012 65022 65031 65038 65032 65039/; my $cv = AnyEvent->condvar; foreach my $port (@ports) { foreach my $host (@hosts) { print "[*] $host:$port\n"; $cv->begin; check_http($port, $host); } } $cv->recv; sub check_http { my ($port_number, $host) = @_; my $ret_val; $host = 'http://' . $host; http_get $host, persistent => 0, on_prepare => sub { my ($fh) = @_; bind($fh, sockaddr_in($port_number, INADDR_ANY)) or print "bind: $!\n"; 15 }, sub { my ($body, $hdr) = @_; if ($hdr->{Status} =~ /^2/) { print "Seems OK! (:\n"; } else { print "ERROR, $hdr->{Status} $hdr->{Reason}\n"; } print "[X] $host:$port_number\n"; $cv->end; }; }

      Ah, now I see the problem. I couldn't create exact test conditions, so simplified script a bit. There were two problems: first, you forgot to set SO_REUSEADDR, second, AnyEvent::HTTP under the hood tried to create second connection to the server with the same source/destination ports, this of course couldn't work (thanks to strace for the help), I fixed it by setting persistent to 1. So here's the script that worked for me, hope it will help:

      #!/usr/bin/perl use strict; use warnings; use AnyEvent; use AnyEvent::HTTP; use AnyEvent::Socket qw( tcp_connect ); use Socket; my @hosts = qw(host1 host2); my @ports = qw(80); my $cv = AnyEvent->condvar; foreach my $port (@ports) { foreach my $host (@hosts) { print "[*] $host:$port\n"; $cv->begin; check_http(1111, $host); } } $cv->recv; sub check_http { my ($port_number, $host) = @_; my $ret_val; $host = 'http://' . $host; http_get $host, persistent => 1, on_prepare => sub { my ($fh) = @_; setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, 1) or warn "Coul +dn't set SO_REUSEADDR"; bind($fh, sockaddr_in($port_number, INADDR_ANY)) or print "bind: $!\n"; 15 }, sub { my ($body, $hdr) = @_; if ($hdr->{Status} =~ /^2/) { print "Seems OK! (:\n"; } else { print "ERROR, $hdr->{Status} $hdr->{Reason}\n"; } print "[X] $host:$port_number\n"; $cv->end; }; }

        Thanks! it worked flawlessly! (:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2015-07-03 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (56 votes), past polls