7stud has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I'm trying to send a GET request to localhost in an attempt to fetch a perl script in my Apache cgi-bin directory.
Here is what I have for the following variables:
my $host = 'localhost'; my $path = '/cgi-bin/second.pl'; my $port = 80;
If I use the short form of IO::Socket::INET->new() and create a socket like this:
my $CONN = IO::Socket::INET->new("$host:$port") or die "Couldn't connect: $!";
I am able to send a request:
my $req = "GET $path HTTP/1.0 $CRLF$CRLF"; $CONN->syswrite($req);
...and then I can read the header that the server sends back just fine:
$/ = CRLF . CRLF; #To read the whole header as one 'line' binmode $CONN; #Turn off \n conversion for getline() in next line my $header = $CONN->getline; $header =~ s/$CRLF/\n/g; say $header; --output:-- HTTP/1.1 200 OK Date: Sun, 21 Feb 2010 12:28:24 GMT Server: Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.7l DAV/2 PHP/5.2 +.3 mod_python/3.3.1 Python/2.4.4 Content-Length: 294 Connection: close Content-Type: text/html
However, if I keep everything the same, and I try to create the socket using the long format:
my $CONN = IO::Socket::INET->new( Peerhost => $host, Peerport => 'http(80)', ) or die "Couldn't connect: $!";
I get the following errors:
Use of uninitialized value $header in substitution (s///) at 4perl.pl +line 55. Use of uninitialized value $header in say at 4perl.pl line 56.
I also tried using just 80 for the Peerport, and I get the same result. Can anyone tell me why I am unable to retrieve the header using the long format for IO::Socket::INET->new()?
Thanks
Here's the full program:
use strict; use warnings; use 5.010; use IO::Socket qw{ :DEFAULT :crlf }; my $host = 'localhost'; my $path = '/cgi-bin/second.pl'; my $port = 80; =pod my $CONN = IO::Socket::INET->new("$host:$port") or die "Couldn't connect: $!"; =cut my $CONN = IO::Socket::INET->new( Peerhost => $host, Peerport => 'http(80)', ) or die "Couldn't connect: $!"; my $req = "GET $path HTTP/1.0 $CRLF$CRLF"; $CONN->syswrite($req); $/ = CRLF . CRLF; #To read whole header as one 'line' binmode $CONN; #turn off \n conversion for getline() below my $header = $CONN->getline; $header =~ s/$CRLF/\n/g; say $header; $CONN->close;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Socket::INET->new(): short form v. long form
by BrowserUk (Patriarch) on Feb 21, 2010 at 13:15 UTC | |
|
Re: IO::Socket::INET->new(): short form v. long form
by Anonymous Monk on Feb 21, 2010 at 13:07 UTC | |
by 7stud (Deacon) on Feb 21, 2010 at 15:39 UTC | |
|
Re: IO::Socket::INET->new(): short form v. long form
by 7stud (Deacon) on Feb 21, 2010 at 15:36 UTC |