【问题标题】:Perl While Loop to send HTTP RequestsPerl While 循环发送 HTTP 请求
【发布时间】:2013-04-10 09:41:11
【问题描述】:

我需要帮助编写一个脚本来发送 25 个请求,然后关闭套接字并启动一个新的。

这是我目前所拥有的:

while ($count<25) {
    $count++;
    die "Could not create socket: $!\n" unless $sock;
    print $sock "GET / HTTP/1.1\r\n";
    print $sock "Host: $host \r\n";
    print $sock "Cookie: $rand \r\n\r\n";
    print while <$sock>;
    close($sock);
};

所以在 25 个请求之后,它会关闭套接字并启动另一个(有点像 goto 命令)。

【问题讨论】:

  • 或许您应该考虑为此目的使用一个模块,例如LWP::UserAgent。与使用套接字相比,它很可能会让您的生活更轻松。

标签: perl http http-post http-get


【解决方案1】:

听起来你有某种(foreach 或 while)循环

while (...) {
   ... Uses $socket ...
}

并且您想将相同的 $socket 用于 25 次通行证,然后切换到新的通行证。因此,只需计算传递次数,如果经过的传递次数可被 25 整除,则更新 $socket。

my $socket;
my $count = 0;
while (...) {
   if (($count++ % 25) == 0) {
      $socket = ...;
   }

   ... Uses $socket ...
}

【讨论】:

  • 我实际上想要这样的东西:1)打开套接字,2)发送 25 个请求,3)关闭套接字,4)循环
  • @Mohamed Gamrah,您总是发送相同的 25 个请求吗?无条件循环(而不是对列表的每个元素重复特定次数,或者直到条件为真)? while (1) { my $socket = ...; ... send requests ...; }
猜你喜欢
  • 2014-02-14
  • 1970-01-01
  • 2019-03-04
  • 2022-01-15
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多