【问题标题】:PERL LWP::Parallel::UserAgent with proxy and custom user agent具有代理和自定义用户代理的 PERL LWP::Parallel::UserAgent
【发布时间】:2016-10-28 11:58:17
【问题描述】:

我无法更改用户代理并通过代理进行并行连接。这是我的简单脚本:

use HTTP::Request; 
use LWP::ConnCache;
use LWP::Parallel::UserAgent;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; 


my $reqs = [  
     HTTP::Request->new('GET', 'https://website1.com'),
     HTTP::Request->new('GET', 'https://website2.com'),
     HTTP::Request->new('GET', 'https://website3.com'),
     HTTP::Request->new('GET', 'https://website4.com'),
     HTTP::Request->new('GET', 'https://website5.com'),
     HTTP::Request->new('GET', 'https://website6.com'),
];



my ($req,$res);

# register requests
foreach $req (@$reqs) {
    print "Registering '".$req->url."'\n";

    $ua = LWP::Parallel::UserAgent->new();
    $ua->duplicates(0);
    $ua->timeout(30);        
    $ua->redirect(1);
    $ua->agent("Test Service v1");  #this doesn't work

    my $proxy_server = '192.168.10.10:8080';
    $ua->proxy(['https', 'http', 'ftp'] => $proxy_server);

    $ua->register ($req , \&handle_answer);
}

my $entries = $ua->wait();




sub handle_answer {
    my ($content, $response, $protocol, $entry) = @_;

    print "Handling answer from '",$response->request->url,": ",
          length($content), " bytes, Code ",
          $response->code, ", ", $response->message,"\n";

    if (length ($content) ) {
        $response->add_content($content);
    } else {

    }


    return undef;
}

我发现更改用户代理的唯一方法是通过模块 HTTP::Headers 并更改 http 请求。

use HTTP::Headers;
....
...
my $headers = new HTTP::Headers(
    'User-Agent' => "Test Service v1",
); 

my $reqs = [  
     HTTP::Request->new('GET', 'https://website1.com', $headers),
     ...
];

...

但我无法通过代理发出请求... 感谢您的帮助

【问题讨论】:

    标签: perl proxy lwp-useragent


    【解决方案1】:

    也许有更好的解决方案,但我会使用线程:

    #!/usr/bin/perl
    
    use threads;
    use LWP::UserAgent;
    
    my $ua = LWP::UserAgent->new();
       $ua->agent("MY USER AGENT");
       $ua->proxy(['https', 'http', 'ftp'] => "http://192.168.10.10:8080");
    
    my @urls = (
     'https://website1.com',
     'https://website2.com',
     'https://website3.com'
    );
    
    my @threads;
    for (@urls) {
       push @threads, async { $ua->get($_) };
    }
    
    for my $thread (@threads) {
       my $response = $thread->join;
       if ($response->is_success) {
           print $response->status_line, "\n";
       }
    }
    

    【讨论】:

    • 感谢您的回复。我之前已经尝试过多线程,就像你在这里写的一样,但是内存和 cpu 的使用率很高,相反 LWP::Parallel 似乎工作得更好
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2016-06-16
    • 2011-06-02
    • 2014-05-14
    • 1970-01-01
    • 2012-09-13
    相关资源
    最近更新 更多