perl - WWW::Mechanize::Timed https 超时不起作用

标签 perl https timeout try-catch www-mechanize

所以我已经在互联网的两端研究了这个问题(至少我是这么认为的)。我正在尝试为 get() 设置 60 秒的 alarm 超时,但它不会被捕获,并且会运行超过 60 秒,而且任何时候默认超时也是如此www::mechanized::timed 构造函数达到(180 秒),我收到以下错误:

在/usr/lib/perl5/site_perl/5.10.0/WWW/Mechanize/Timed.pm 第 52 行使用未初始化值 (+)。

代码:

use WWW::Mechanize::Timed;
use HTTP::Cookies;
use Try::Tiny;

my $ua = WWW::Mechanize::Timed->new(
autocheck => 0#turning off autocheck becuase any get errors will be fatal need to check ourselves
);

my $cookies = HTTP::Cookies->new(
autosave => 1
);

$ua->cookie_jar($cookies);

$ua->agent_alias("Windows IE 6");

try{
local $SIG{ALRM} = sub { die "alarm\n" };
alarm 60;
$ua->get('https://secure.site.com'); #secure site that timed out
alarm 0;
} catch {
die $_ unless $_ eq "alarm\n";
print "page timed out after 60 seconds!\n";
exit;
};

my $total_time = sprintf '%.3f', ($ua->client_elapsed_time);

unless($ua->success){
print "Error: " . $ua->status;
exit;
}
...

我已经仔细研究了这些问题,以找出如何在不编写自己的超时函数的情况下让闹钟工作。

Perl Mechanize timeout not working with httpsWays to do timeouts in Perl?

到目前为止,我看到了使用 LWPx::ParanoidAgent 的建议,不确定我是否理解“使用 LWPx::ParanoidAgent 并将其混合到 Mech 中”部分

Possible to use timeout in WWW::Mechanize on https?

或用 LWP::UserAgent 修补

http://search.cpan.org/~sharyanto/LWP-UserAgent-Patch-HTTPSHardTimeout-0.04/lib/LWP/UserAgent/Patch/HTTPSHardTimeout.pm

关于如何让超时与警报一起工作有什么想法吗?

谢谢!

最佳答案

下面的内容有助于为每个 get() 设置警报,似乎比使用 sig alarm 进行 try-catch 容易得多,除非我错过了一些东西?

use Sys::SigAction qw(timeout_call);

if ( timeout_call( 60 ,sub { $ua->get('https://secured.site.com'); } ))
   {
print "ALARM page timed out after 60 seconds!\n" ;
exit;
}

与这个问题的答案几乎相同,但使用实际代码 Ways to do timeouts in Perl?

文本来自http://metacpan.org/pod/Sys::SigAction

timeout_call()

$timeout ,$coderef

Given a code reference, and a timeout value (in seconds), timeout() will (in an eval) setup a signal handler for SIGALRM (which will die), set an alarm clock, and execute the code reference. $time (seconds) may be expressed as a floating point number.

If Time::HiRes is present and useable, timeout_call() can be used with a timer resolution of 0.000001 seconds. If Time:HiRes is not available then factional second values less than 1.0 are tranparently converted to 1.

If the alarm goes off the code will be interrupted. The alarm is canceled if the code returns before the alarm is fired. The routine returns true if the code being executed timed out. (was interrupted). Exceptions thrown by the code executed are propagated out.

The original signal handler is restored, prior to returning to the caller.

If HiRes is not loadable, Sys::SigAction will do the right thing and convert

最后要考虑/记住的事情:

use of Sys::SigAction::timeout_call unsafe?

关于perl - WWW::Mechanize::Timed https 超时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12964503/

相关文章:

windows - 如何阻止 Perl 程序在完成后关闭其窗口?

sql-server - 如何在与我所在的服务器不同的服务器上运行 perl 脚本(通过 BAT 文件)?

perl - 文件处理

javascript - jQuery:防止动画和超时排队?

ruby-on-rails - Heroku 上的 H12 超时错误

arrays - <> 生成的数组大小?

c# - 使用 asp.net 和 C# 从 javascript 调用外部服务器上的 webservice

amazon-web-services - 443 以外端口的 AWS 虚拟机 Https

java - Debian SSL 证书错误 : cannot verify xxx certificate/javax.net.ssl.SSLHandshakeException

c# - ADO.NET 的 SqlCommand.CommandTimeout 是如何工作的?