Prints the response to an HTTP request. The URL is specified on the command line.
#!/usr/bin/perl -w
# get_url.pl prints the response for an HTTP request. It is
# invoked with a URL as the only mandatory argument. Better
# use LWP if available.
#
# Copyright 2003, Ramiro Gómez.
#
# This program is free software; you can redistribute
# it and/or modify it under the same terms as Perl itself.
use strict;
use IO::Socket qw(:DEFAULT :crlf); # import constants
# die if no argument was given
die "Usage: $0 URL\n" unless @ARGV;
my $uri = shift;
# see regex for URIs in perldoc URI (URI.pm required)
my( $host, $path ) = $uri =~ m|^http://([^/?#]*)?([^?#]*)|
or die "The URL supplied is not accepted.
\nExample: http://www.ramiro.org/";
# open the socket connection
my $socket = IO::Socket::INET->new( PeerAddr => $host,
PeerPort => 'http(80)')
or die "Can't connect: $:";
# send request
print $socket "GET $path HTTP/1.0", CRLF, CRLF;
# print the response
my $data;
# byte-oriented read since resource may consist of binary data
while ( read( $socket, $data, 1024 > 0 ) ) {
print $data;
}
Post new comment