tags:

views:

551

answers:

3

I want to use a proxy with this perl script but I'm not sure how to make it use a proxy.

#!/usr/bin/perl
use IO::Socket;
$remote = IO::Socket::INET->new(
                        Proto    => "tcp",
                        PeerAddr => "localhost",
                        PeerPort => "8080",
                    )
                  or die "cannot connect";
print $remote "GET / HTTP/1.0\n\n";
    while ( <$remote> ) { print }
+6  A: 

Use the LWP::UserAgent module, which has built-in proxy support.

AmbroseChapel
+1. Much easier than trying to do it yourself.
rjp
I'd rather not use LWP::UserAgent though. -sigh-
Hintswen
@Hintswen - What's the reason you'd rather not the LWP::UserAgent workable solution - why the sigh?
John K
Well actually it's mainly because I know that if I learn ho to do it with IO::Socket I will be able to do it from telnet basically, or from anything I can send raw data with (or am I wrong?)Alsi I know I can connect to this particular service using IO::Socket.What I am trying to get isn't a webpage. (but I will try it soon and figure out if it works)
Hintswen
+1  A: 

Straight from one of my scripts:

use LWP::UserAgent;
my($ua) = LWP::UserAgent->new;

if ($opts->{'proxy'}) {
    my($ip) = Sys::HostIP->hostip;
    if (($ip =~ m{^16\.143\.}) ||
     ($ip =~ m{^161\.}) ||
     ($ip =~ m{^164\.})) {
     $ua->proxy(http  => 'http://localhost:8080');
    }
    else {
     $ua->proxy(http  => "");
    }
}
else {
    $ua->env_proxy;
}

#***** get current entry *****
my($req) = HTTP::Request->new(GET => "http://stackoverflow.com/questions/1746614/use-proxy-with-perl-script");
my($raw) = $ua->request($req)->content;
Joe Casadonte
A: 
  use LWP::UserAgent;
  $ua = LWP::UserAgent->new;
  $ENV{HTTP_proxy} = "http://ip:port";
  $ua->env_proxy; # initialize from environment variables
  my $req = HTTP::Request->new(GET => 'http://google.com/');
  print $ua->request($req)->as_string;
  delete $ENV{HTTP_PROXY};
zee