2009-11-17 5 views
4

이 펄 스크립트로 프록시를 사용하고 싶지만 프록시를 사용하는 방법을 잘 모르겠습니다.펄 스크립트로 프록시 사용

#!/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 } 
+0

아직도 비 LWP 솔루션 – Hintswen

답변

9

proxy support이 내장 된 LWP :: UserAgent 모듈을 사용하십시오. 내 스크립트 중 하나에서

+0

일을 찾고. 직접하는 것보다 훨씬 쉽습니다. – rjp

+0

나는 LWP :: UserAgent를 사용하지는 않을 것이다. -sigh- – Hintswen

+0

@Hintswen - 왜 LWP :: UserAgent 실행 가능한 솔루션이 아닌지 이유 - 왜 그럴까요? –

1

스트레이트 :

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; 
7
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};