【问题标题】:Explicitly close a connection established by LWP::Simple显式关闭由 LWP::Simple 建立的连接
【发布时间】:2017-12-26 14:16:46
【问题描述】:

我有一个使用 getprint 的 Perl 脚本 LWP::Simple 获取一些 JSON 数据。

我们看到两台服务器上的 TCP 连接都保持打开状态。

我想确保当getprint 完成时 Perl 会显式关闭连接。有没有办法做到这一点?

【问题讨论】:

  • 已编辑 - 我对 Web 服务器通信还很陌生。该脚本仅查询特殊 URL,该 URL 包含类似“?serial=59595”之类的内容,我认为这是一个“POST”。
  • 不,事实上,POST 请求往往没有 url 参数。
  • 虽然 LWP::UserAgent(由 LWP::Simple::getprint 使用)确实允许 keep_alive 或连接缓存,但默认情况下不应发生

标签: perl tcp lwp


【解决方案1】:

虽然有人指出 LWP::Simple 不应该不经要求就保持连接打开,但我建议使用LWP::UserAgent。然后,您可以简单地让对象超出范围:

{
    # Set the timeout to some value you find reasonable
    my $ua = LWP::UserAgent->new( timeout => 30 );
    # open connection, do what you wanted to use it for
    # ...
    # will run out of scope now. This may not be necessary,
    # but now you can be quite certain the connection is dropped.

}

oo 接口还允许 (imo) 更简单地配置超时等内容。

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2019-12-14
    相关资源
    最近更新 更多