slloyd has asked for the wisdom of the Perl Monks concerning the following question:
#!perl use strict; use Socket; my $url="http://www.cpan.org/misc/jpg/cpan.jpg"; my $host="www.cpan.org"; $|=1; my $start=times; my ( $iaddr, $paddr, $proto ); $iaddr = inet_aton( $host ); $paddr = sockaddr_in( 80, $iaddr ); $proto = getprotobyname( 'tcp' ); unless( socket( SOCK, PF_INET, SOCK_STREAM, $proto ) ) {die "ERROR Dud +e: getUrl socket: $!";} unless( connect( SOCK, $paddr ) ) {die "getUrl connect: $!\n";} my @head=( "GET $url HTTP/1.1", "User-Agent: Mozilla/4.78 [en] (X11; U; Safemode Linux i386)", "Pragma: no-cache", "Host: $host", "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, imag +e/png, */*", "Accept-Language: en" ); push(@head,"",""); #Build Header and print to socket my $header=join("\015\012",@head); print "sending request\n$header\n\n"; select SOCK; $| = 1; binmode SOCK; print SOCK $header; my $body=''; while( <SOCK> ) { my $data=$_; $data=~s/[\r\n\t]+$//s; $data=~s/^[\r\n\t]+//s; last if $data=~/^0$/s; my $len=length($data); #print STDOUT "len:$len\n"; $body .= $data; last if $data=~/\<\/html\>$/is; if($data=~/\<\/body\>$/is){ $body .= qq|</html>|; last; } #print STDOUT "$data\n"; } unless( close( SOCK ) ) { return ( "getUrl close: $!" ); } select STDOUT; close SOCK; my $end=times; my $diff=$end=$start; print "Took $diff to access page\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: using Socket to get urls hangs for several seconds.
by Errto (Vicar) on Feb 15, 2005 at 04:56 UTC | |
|
Re: using Socket to get urls hangs for several seconds.
by merlyn (Sage) on Feb 15, 2005 at 04:54 UTC | |
|