User Tools

Site Tools


perl:single_document_web_server

This little script acts as a web server, but it serves only one document no matter what the requested URL was: the document embedded in the script itself. This is useful to throw on a machine in a lab environment to identify who owns it and what it's for.

#!/usr/bin/perl
use IO::Socket;
use strict;
 
# single-document web server
# by Tyler Bletsch
 
my $listenPort = 80;
my $document = join('',<DATA>);
 
my $sockListen = new IO::Socket::INET (LocalPort => $listenPort, Proto => 'tcp', Listen => 10, Reuse => 1);
die "Could not create socket: $!\n" unless $sockListen;
 
#print "Listening on port $listenPort.\n";
 
while (1) {
	#print "Waiting to accept on port $listenPort...\n";
	my $sockClient = $sockListen->accept(); # Get next client
	scalar <$sockClient>; # humor the client; read one line of request before responding.
 
	print $sockClient "HTTP/1.0 200 Okay, pal\n";
	print $sockClient "Content-type: text/html\n\n";
	print $sockClient $document;
	close $sockClient;
}
 
__DATA__
<html><body>
 
Your document goes here!
 
</body></html>
perl/single_document_web_server.txt · Last modified: 2009/08/13 09:56 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki