English 中文(简体)
将代理添加到 Perl 代码
原标题:Add proxy to Perl code
  • 时间:2012-05-24 18:36:55
  •  标签:
  • perl
  • proxy

我试图在我的perl代码中添加一个代理代码, 但我不确定这是否是正确的代码?

#!usr/bin/perl

{

use strict;
 use LWP::UserAgent;
use warnings;
 my $ua = new LWP::UserAgent(agent =>  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5 );
 $ua->proxy([qw(http https)] =>  http://100.100.10.100:80 );
 my $response = $ua->get("URL_IN_HERE");
 print $response->code,   , $response->message,"
";

}
问题回答

在您的环境( 可能是 < code_ /. bashrc 或 类似 ) 中 :

export http_proxy=http://100.100.10.100:80

在 Perl 代码 :

#!/usr/bin/perl

use warnings;
use strict;

use LWP::UserAgent;


my $ua = new LWP::UserAgent(agent =>  Mozilla/5.0 );
$ua->env_proxy;
my $response = $ua->get("URL_IN_HERE");
print $response->code,   , $response->message,"
";

下面举两个例子,以了解如何...


如果您的代理服务器在端口 127.0.0.1 运行 <8080 并支持 , 然后添加

$ua->proxy([https, https], http://127.0.0.1:8080/ ); to your code.


如果您的代理服务器运行于 proxy.mydomain.com 端口 80 并支持 , 然后添加

$ua->proxy ([http:// http, ftp], http://proxy.mydomain.com.80/]; to your code.


尝试使用下一个代码 :)

#!usr/bin/perl
use strict;
use LWP::UserAgent;
use warnings;
{
my $ua = LWP::UserAgent->new;
    $ua->proxy(  http , "http://127.0.0.1:3128");  #Your proxy
    $ua->default_header("Connection" => "keep-alive");
 $ua->agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.63 Safari/537.31");
 $ua->timeout (10);
 my $response = $ua->get("http://google.com"); #Website
 print $response->code,   , $response->message,"
";

}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...