How-to: Create a simple Perl HTTP server

I came across a situation where I needed to test access through a complex network of firewalls, nat’s and ACL’s to a server that was to be setup. Problem was the server was not ready yet, it was up but the application was yet to be installed. The application when installed listened on port 4321 for clients. So I needed a way to test connectivity over port 4321 to a server that had nothing listening. ICMP is not permitted, so I couldn’t even ping to test connectivity.

What I needed was a quick and dirty way to setup a daemon that was listening on port 4321 so I could try and access it. Perl to the rescue! I created a little script that runs a simple perl HTTP server and displays a file. Then from the other end simple browsed to http://IP:4321 and could tell if it was open or not. Here is the code I used to create the simple HTTP daemon on the remote host.


#! /usr/bin/perl

use strict;
use warnings;
use HTTP::Daemon;
use HTTP::Status;

my $httpd = new HTTP::Daemon
LocalPort => 4321;

print "TO connect point your browser to: <URL:", $httpd->url, ">n";
while (my $c = $httpd->accept) {
while (my $r = $c->get_request) {
if ($r->method eq 'GET' and $r->url->path eq "/") {
$c->send_file_response("/tmp/filenametodisplay.txt");
} else {
$c->send_error(RC_FORBIDDEN)
}
}
$c->close;
undef($c);
}

Thats all there is to it. Let me stress this is for teasting purposes only. You would unwise to run a production web server this way, but it gets the job done!


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *