#!env perl
{
package PythonServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
use Data::Dump 'pp';
# URL paths to monitor
my %dispatch = (
'/fetch' => \&fetch_page,
'/save' => \&save_data,
);
# Look up the path and execute the associated routine, or
# give a typical 404 error
sub handle_request {
my ($self, $cgi) = @_;
my $path = $cgi->path_info();
my $handler = $dispatch{$path};
if ("CODE" eq ref $handler) {
print "HTTP/1.0 200 OK\r\n";
$handler->($cgi);
return;
}
print "HTTP/1.0 404 Not found\r\n",
$cgi->header,
$cgi->start_html("Cheese Shoppe"),
$cgi->h1("Sorry, We're fresh out of that one."),
$cgi->end_html;
}
sub fetch_page {
my $cgi = shift;
return if !ref $cgi;
print STDERR "Fetched page!\n";
open my $FH, '<', 'pm_1231092.html' or die "Can't open file: $!\n";
local $/;
my $t = <$FH>;
print $cgi->header;
print $t;
}
sub save_data {
my $cgi = shift;
return if !ref $cgi;
print STDERR "Got data: ", pp($cgi->{param}), "\n";
}
}
# Start up the server
use strict;
use warnings;
my $pid = PythonServer->new(8088)->run;
print "PID = $pid, press ^C to stop\n";
my $t = <>;
####
A whimsical form
What is the airspeed of a laden swallow?
##
##
$ perl pm_1231092.pl
PythonServer: You can connect to your server at http://localhost:8088/
Fetched page!
Got data: { NumCocoanuts => [1], SwallowType => ["European"] }