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

Hi All, I am using WWW::Mechanize perl module for the testing of the web pages. I am creating virtual Interfaces on my linux system. I want to associate each virtual Interface with the object of WWW::Mechanize. how do we associate virtual Interfaces with object ID? Please help me on this. Regards, Chirag
  • Comment on Using Virtual interface through WWW::Mechanize

Replies are listed 'Best First'.
Re: Using Virtual interface through WWW::Mechanize
by Fletch (Bishop) on Jun 21, 2006 at 12:56 UTC

    You could try something like this old code I had lying around (translation: this is old and I don't know if it still works with recent versions of LWP). Set $ENV{LWP_LOCAL_ADDR} before you create each instance and it'll use that for the local socket address.

    package MyHTTP; use base qw(LWP::Protocol::http); sub _new_socket { my($self, $host, $port, $timeout) = @_; local($^W) = 0; # IO::Socket::INET can be noisy my @args = (PeerAddr => $host, PeerPort => $port, Proto => 'tcp', Timeout => $timeout, ); push @args, LocalAddr => $ENV{LWP_LOCAL_ADDR} if exists $ENV{LWP_LOCAL_ADDR}; my $sock = IO::Socket::INET->new( @args ); unless ($sock) { # IO::Socket::INET leaves additional error messages in $@ $@ =~ s/^.*?: //; die "Can't connect to $host:$port ($@)"; } $sock; } LWP::Protocol::implementor( 'http', 'MyHTTP' ); 1;

    You also could alter it from using %ENV to maybe look at a global instead. I'm not aware of a better way of passing options down through from the LWP::UserAgent level (not to mention from Mechanize) to this level though; would be nice if you got a reference to that instance as an arg to _new_socket.

      _extra_sock_opts should overriden instead of _new_socket for forward compatibility. For example, your solution breaks connection caching.

      package MyHTTP; use base qw(LWP::Protocol::http); sub _extra_sock_opts { my ($self, $host, $post) = @_ my @opts = $self->SUPER::_extra_sock_opts($host, $post); push @opts, LocalAddr => $ENV{LWP_LOCAL_ADDR} if exists $ENV{LWP_LOCAL_ADDR}; return @opts; } LWP::Protocol::implementor( 'http', 'MyHTTP' ); 1;
      or just
      push @LWP::Protocol::http::EXTRA_SOCK_OPTS, LocalAddr => $ENV{LWP_LOCAL_ADDR} if exists $ENV{LWP_LOCAL_ADDR};
Re: Using Virtual interface through WWW::Mechanize
by freakingwildchild (Scribe) on Jun 21, 2006 at 10:40 UTC
    I do this happyly by using the $ENV{HTTP_HOST} variable for incoming requests on my virtual addresses defined in a mapfile through Apache; but have not yet found out how to use LWP::Simple or WWW::Mechanize bound to a different interface.