Sunday, January 10, 2010

PERL TCP Socket Codes without relying on LWP

I don't quite like to use the modules in PERL as I am always paranoid about its absence in certain hosting companies.

Here are the codes to make a TCP connection and send and receive a HTTP GET query without the LWP module:

use Socket;

my $peer = sockaddr_in(80,inet_aton(""));

socket(S, PF_INET, SOCK_STREAM, getprotobyname("tcp"));

connect(S, $peer) or die "Couldn’t connect!";

print S "GET / HTTP/1.1\\n\n"; # Make a GET query to root directory of the host server

select STDOUT;

while (<S>) {
$reply .= $_;
if (/<\/html>/) { close(S); }

#the reply is stored in the $reply

You can also send a query string to the host script by:
print S "GET /cgi-bin/$action&cat=main HTTP/1.1\\n\n";

But for POST query, it is a bit different. Will post up the codes tomorrow!

Today is a Sunny Sunday. I will take some time off to relax. See ya!

No comments:

Post a Comment