You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Perl Mechanize请求报错(Bad file descriptor),如何优化代码?

解决WWW::Mechanize请求FlightAware失败的问题

问题代码

use strict;
use warnings;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
$mech->get("https://www.flightaware.com/live/airport/KCLE");
my $content = $mech->content();

print "$content\n";

报错信息

Error GETing https://www.flightaware.com/live/airport/KCLE: Can't connect to www.flightaware.com:443 (Bad file descriptor) at line 6.

解决方案

以下是几种可行的修改和排查方向:

  • 确保SSL依赖模块安装完整
    WWW::Mechanize的HTTPS请求依赖IO::Socket::SSL模块,缺失该模块会导致SSL连接失败。执行以下命令安装:

    cpan IO::Socket::SSL
    # 或使用cpanm(如果已安装)
    cpanm IO::Socket::SSL
    
  • 显式配置SSL选项
    部分环境下证书验证可能导致连接问题,可以在创建Mechanize对象时调整SSL参数(注意:关闭证书验证存在安全风险,仅用于测试或确认环境问题):

    use strict;
    use warnings;
    use WWW::Mechanize;
    use IO::Socket::SSL;
    
    my $mech = WWW::Mechanize->new(
        ssl_opts => {
            SSL_verify_mode => SSL_VERIFY_NONE,
            verify_hostname => 0,
        },
    );
    $mech->get("https://www.flightaware.com/live/airport/KCLE");
    my $content = $mech->content();
    
    print "$content\n";
    
  • 检查网络代理设置
    如果运行环境处于代理网络下,需要为Mechanize配置代理:

    # 在创建对象后添加代理设置
    $mech->proxy(['http', 'https'], 'http://your-proxy-address:port/');
    
  • 更新相关模块版本
    老旧版本的WWW::Mechanize或LWP::UserAgent可能存在SSL兼容问题,执行以下命令更新:

    cpan WWW::Mechanize LWP::UserAgent
    

内容的提问来源于stack exchange,提问作者Brian Douglas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 16:18:19