Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Direct a LWP connection to a different host

by Corion (Patriarch)
on Jan 30, 2005 at 21:36 UTC ( [id://426436]=CUFP: print w/replies, xml ) Need Help??

You have two servers, A1.com and A2.com, and A2.com is the backup mirror of A1.com. You want to make one HTTP request to A2.com but pretend that the DNS resolves A1.com to its IP. This snippet allows you that, by taking over the creation of new IO::Socket objects.

The correct Host: header for A1.com is still sent, so name-based virtual hosting works as it should, too.

Update: Failover is also a good keyword for this node...

use strict; use IO::Socket::INET; use LWP::Simple; my $url = 'http://www.google.com'; my $backup_host = 'www.apache.org'; getprint $url; { my $old_constructor = \&IO::Socket::INET::new; local *IO::Socket::INET::new = sub { my ($package,%args) = @_; warn "Redirecting from $args{PeerAddr} to $backup_host"; $args{PeerAddr} = $backup_host; $old_constructor->($package,%args); }; getprint $url; };

Replies are listed 'Best First'.
Re: Direct a LWP connection to a different host
by Aristotle (Chancellor) on Jan 30, 2005 at 22:57 UTC

    You could also override &CORE::GLOBAL::gethostbyname for a more implementation-agnostic approach. It's slightly less code, too.

    Makeshifts last the longest.

Re: Direct a LWP connection to a different host
by hossman (Prior) on Jan 31, 2005 at 07:25 UTC

    if your only concern is LWP, then you can do this on a per UA basis using proxy settings...

    use LWP::UserAgent; my $u = LWP::UserAgent->new; $u->proxy("http","http://www.apache.org/" +); print $u->get("http://google.com/")->as_string;

    UPDATE: your milage may vary based on the HTTP Server you connect to, see followup replies for details

      How can that work? HTTP requests sent to proxies differ from those sent to destination hosts.

      Makeshifts last the longest.

        This should work too, at least somewhat, because the main difference between the proxy request and the real request is that the GET request is changed from a relative to an absolute URL:

        Host: www.google.com GET http://www.google.com/

        instead of

        Host: www.google.com GET /

        But yes, that somewhat invalidates the intent to just redirect the socket connection and leave everything else as-is, to fake a different name resolution as there actually is.

Re: Direct a LWP connection to a different host
by thospel (Hermit) on Jan 31, 2006 at 15:36 UTC
    Another way is this:
    #!/usr/bin/perl -w use strict; use LWP::Simple; my $url = 'http://www.google.com'; my $backup_host = 'www.apache.org'; @LWP::Protocol::http::EXTRA_SOCK_OPTS = (PeerAddr => $backup_host); getprint $url;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found