Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Telling LWP which source IP to use

by geekondemand (Scribe)
on Feb 18, 2005 at 19:02 UTC ( [id://432472]=perlquestion: print w/replies, xml ) Need Help??

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

I'm writing a script that does some scraping using LWP (and other modules that use LWP). The box I'm running this on has a bunch of IP addresses assigned to it. Is there a way in which I can tell LWP which IP address to use to do it's work? I can't seem to find this in the LWP documentation and ask you, oh wise monks, to impart your wisdom on this matter so I can grow in laziness, impatience and hubris.

Retitled by davido from 'Telling LWP which pipe to use'.

Replies are listed 'Best First'.
Re: Telling LWP which source IP to use
by Kanji (Parson) on Feb 18, 2005 at 19:30 UTC

    It's not documented in the POD, but...

    @LWP::Protocol::http::EXTRA_SOCK_OPTS = (LocalAddr => $ip_to_use);

    However, I don't recall if it works for all protocols that LWP supports, and may be something that is deprecated in future releases.

        --k.


Re: Telling LWP which source IP to use
by Fletch (Bishop) on Feb 18, 2005 at 19:26 UTC

    This was something I came up with ages ago. Don't know if it'll play nice with current LWP or not, but it should get you started.

    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' ); package main; use LWP::Simple qw( getprint ); getprint( shift || 'http://localhost/' ); exit 0; __END__
Re: Telling LWP which source IP to use
by friedo (Prior) on Feb 18, 2005 at 19:11 UTC
    That is a function of the routing tables in your operating system, not the application. If you want LWP to use a specific IP for outgoing connections, you'll have to change the network interface used for the address range you're trying to connect to.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found