basic tiny webserver. Actually it could use a good round of golf. Signal handlers can also be added to take care of HUP,INT and TERM to safely reset or kill the daemon.
local $|=1;
use IO::Socket;
my $PORT = $ARGV[0] || 9000; # pick something not in
+use
my $server = IO::Socket::INET->new( Proto => 'tcp',
LocalPort => $PORT,
Listen => SOMAXCONN,
LocalAddr => 'localhost',
Reuse => 1);
unless ($server) {
logit "can't setup server",0;
exit 0;
}
while (my $client = $server->accept()) {
$client->autoflush(1);
my $request = <$client>;
chomp $request;
#do stuff with the request here, like:
if ($request =~ m|^GET /(.*) HTTP/1.[01]|) {
# some sort of processing, whatever you want
} else {
print $client "HTTP/1.0 400 BAD REQUEST\n";
print $client "Content-Type: text/plain\n\n";
print $client "BAD REQUEST ($request)\n";
}
close $client;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.