Use proxy with perl script

后端 未结 3 1035
青春惊慌失措
青春惊慌失措 2021-01-06 11:12

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(         


        
相关标签:
3条回答
  • 2021-01-06 11:44

    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;
    
    0 讨论(0)
  • 2021-01-06 11:50

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

    0 讨论(0)
  • 2021-01-06 12:07
      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};
    
    0 讨论(0)
提交回复
热议问题