get_url.pl

Tagged:  •    •    •    •    •    •  

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

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br>

More information about formatting options