gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:58:49 25 4
gpt4 key购买 nike

所以我已经在互联网的两端研究了这个问题(至少我是这么认为的)。我正在尝试为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com