multithreading - How can use Threads in Requesting Perl -
i'm posting simple script : 1 open text file multiple urls site. 2 first line gets address of site , makes request. 3 content verified on condition. threads want add script make faster, not know how do. please me in threads ?
#!/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; ($i = 0; $i <= $numebers; $i++) { $url = "$array[$i]"; $req=http::request->new(get=>$url); $ua=lwp::useragent->new(); $ua->timeout(15); $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 ! };
#!/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; $output_lock :shared; $output_fh; sub write_to_output_file { lock($output_lock); print($output_fh @_); $output_fh->flush(); } sub worker { ($ua, $url) = @_; $response = $ua->get($url); write_to_output_file("$url\n") if $response->success && $response->content =~ /exist/; } { $output_fh = \*stdout; # or open file. $q = thread::queue->new(); (1..num_workers) { async { $ua = lwp::useragent->new( timeout => 15 ); while (my $job = $q->dequeue()) { worker($ua, $job); } }; } while (<>) { chomp; $q->enqueue($_); } $q->end(); $_->join() threads->list(); }
Comments
Post a Comment