in reply to Re^2: How to specify outlet interface for LWP?
in thread How to specify outlet interface for LWP?
?? bullpucky
#!/usr/bin/perl -- use strict; use warnings; use LWP 5.826; use LWP::Protocol::http; #perldoc -q "How do I find out my hostname/domainname/IP address?" use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); my $ua = LWP::UserAgent->new(); { print "# works like default\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => $addr ) +; print $ua->get("http://www.example.com")->status_line,"\n"; } { print "# bad hostname\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => "nohost +:666" ); print $ua->get("http://www.example.com")->status_line,"\n"; } { print "# unknown error\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => "localh +ost" ); print $ua->get("http://www.example.com")->status_line,"\n"; printf "$^E\n"; } __END__ # works like default 200 OK # bad hostname 500 Can't connect to www.example.com:80 (Bad hostname 'nohost:666') # unknown error 500 Can't connect to www.example.com:80 (connect: Unknown error) A connection attempt failed because the connected party did not proper +ly respond after a period of time, or established connection failed because connected host has failed to respond
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to specify outlet interface for LWP?
by sunshine_august (Scribe) on May 25, 2009 at 06:49 UTC |