首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Perl:无法为HTTPS网站使用LWP::Parallel::UserAgent

Perl:无法为HTTPS网站使用LWP::Parallel::UserAgent
EN

Stack Overflow用户
提问于 2019-03-10 08:45:26
回答 1查看 461关注 0票数 1

我无法为https网站使用perl模块LWP::Parallel::UserAgent。下面是我使用的代码:

代码语言:javascript
运行
复制
#!/usr/bin/perl

use LWP::Parallel::UserAgent qw(:CALLBACK);
use HTTP::Request; 

my $BrowserName = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";

my $pua = LWP::Parallel::UserAgent->new();
$pua->agent( $BrowserName );
$pua->nonblock('true');
$pua->in_order  (1);
$pua->duplicates(0);
$pua->timeout   (10);
$pua->redirect  (2);
$pua->remember_failures ( 1 );

$url = "https://www.squeezemind.it";

my $res = $pua->register( HTTP::Request->new('GET', $url), \&gestione_risposta, 4096 );

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

# Leggo le risposte
foreach (keys %$entries) {
  my $res = $entries->{$_}->response;
  print "\n\nAnswer for '",$res->request->url, "' was ", $res->code,": ", $res->message;  
}


sub gestione_risposta {

  my($html_content, $response, $protocol, $entry) = @_;

  if( !$response->is_success || $response->code != 200 ) { return C_ENDCON; }  

  if( length($html_content) ) {    
    # Bla Bla
  }

  return undef; 

}

它对于http很好,但是如果您试图用https网站更改$url,它就失败了。

对于https://www.squeezemind.it

错误代码: 500条消息:无法通过包IO::Socket::INET在/usr/share/perl5 5/LWP/Protocol/https.pm第119行中定位对象方法"get_cipher“

对于https://www.stackoverflow.com

错误代码: 402条消息:读取响应时出现意外EOF

这个系统是最新的。有什么建议吗?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2019-03-10 19:16:53

从你的代码:

代码语言:javascript
运行
复制
$pua->nonblock('true');

当查看LWP::Parallel::UserAgent的代码时,它似乎完全破坏了对HTTPS的非阻塞支持:https支持是在LWP::Parallel:Protocol::https中实现的,它派生于LWP::Parallel::Protocol::http for 执行实际连接连接中的相关代码

代码语言:javascript
运行
复制
103     unless ($nonblock) {
104       # perform good ol' blocking behavior
105       #
106       # this method inherited from LWP::Protocol::http
107       $socket = $self->_new_socket($host, $port, $timeout);
108       # currently empty function in LWP::Protocol::http
109       # $self->_check_sock($request, $socket);
110     } else {
111       # new non-blocking behavior
...
116       $socket =
117         IO::Socket::INET->new(Proto => 'tcp', # Timeout => $timeout,
118                               $self->_extra_sock_opts ($host, $port));

可以看到,对于(默认)阻塞情况,代码使用LWP::Protocol::http的功能,但对于非阻塞情况,它直接使用IO::Socket::INET -而不是HTTPS的IO::Socket::SSL。但是LWP::Protocol::http (稍后使用)实际上需要一个SSL套接字,并试图在它上调用get_cipher。这将导致您看到的错误:

无法通过包"IO::Socket::INET“在/usr/get_cipher/perl5 5/LWP/Protocol/https.pm第119行找到对象方法”INET“

当不使用非阻塞支持时,代码似乎会起作用。

对于本模块中的一般HTTPS,请参见README.SSL

代码语言:javascript
运行
复制
 ** DISCLAIMER: https support is pretty buggy as of now. i haven't **
 ** had time to test much of it, so feel free to see if it works   **
 ** for you, but don't expect it to :-)  

换句话说:您可能应该使用不同的模块来获得对HTTPS的可靠支持。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55085951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档