如何在请求Perl中使用线程

时间:2015-05-14 18:56:44

标签: multithreading perl request

我发布这个简单的脚本: 1打开一个包含多个站点URL的文本文件。 2第一行获取站点的地址并发出请求。 在条件上验证了3个内容。 线程想要添加此脚本以加快速度,但不知道该怎么做。请有人在线程中帮助我吗?

    #!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Request;
use LWP::Simple;

$sis="$^O";if ($sis eq linux){ $cmd="clear";} else { $cmd="cls"; }
system("$cmd");

if (!$ARGV[0]) {

$sis="$^O";if ($sis eq linux){ $cmd="clear";} else { $cmd="cls"; }
system("$cmd");

print q {
[+] Request.....

};

open( SITE, "< $ARGV[0]" ) or die( "No open: $!" );

our @array = <LINES>;

$numbers = $#array;

for ($i = 0; $i <= $numebers; $i++) {

$url = "$array[$i]";

my $req=HTTP::Request->new(GET=>$url);
my $ua=LWP::UserAgent->new();
$ua->timeout(15);
my $resposta=$ua->request($req);

if($resposta->content =~ /Exist/) {

print "n t $url n";

open (NOTEPAD, ">> texto.txt");
print NOTEPAD "$url";
close(NOTEPAD);

}
}

print q {

[+] FIM !
};

1 个答案:

答案 0 :(得分:1)

#!/usr/bin/perl
use strict;
use warnings;
use threads;
use threads::shared;

use Fcntl              qw( LOCK_EX );
use IO::Handle         qw( );
use LWP::UserAgent     qw( );
use Thread::Queue 3.01 qw( );

use constant NUM_WORKERS => 20;

my $output_lock :shared;
my $output_fh;
sub write_to_output_file {
   lock($output_lock);
   print($output_fh @_);
   $output_fh->flush();
}

sub worker {
   my ($ua, $url) = @_;
   my $response = $ua->get($url);
   write_to_output_file("$url\n")
      if $response->success
      && $response->content =~ /Exist/;       
}

{
   $output_fh = \*STDOUT;  # Or open a file.

   my $q = Thread::Queue->new();
   for (1..NUM_WORKERS) {
      async {
         my $ua = LWP::UserAgent->new( timeout => 15 );
         while (my $job = $q->dequeue()) {
            worker($ua, $job);
         }
      };
   }

   while (<>) {
      chomp;
      $q->enqueue($_);
   }

   $q->end();
   $_->join() for threads->list();
}