Here's something super cheezy I just wrote up as a quick excersize for myself. It does what you ask for...

use strict; use IO::Socket; my $server = IO::Socket::INET->new ( Proto => "tcp", LocalPort => 3000, Reuse=>1, Listen=>10 ); my ( $pid, $host, $date ); $date = scalar localtime(); sub spawn_srvr { my ( $vers, $page, %hdrs ); while (<$host>) { /^(GET)\s(.*)/ && do { $hdrs{$1} = $2; next; }; /^((\w|-)+):(.*)/ && do { $hdrs{$1} = $3; next; }; /\n/ and do { ( $page, $vers ) = split(/\s/, $hdrs{'GET'}, 2); if ( open(FH, "<$page") ) { print $host <<EOF; HTTP/1.1 200 OK Date: $date Server: LameWeb Connection: close Content-Type: text/html EOF print $host $_ while ( <FH> ); } else { print $host <<EOF; HTTP/1.1 404 Not Found Date: $date Server: LameWeb Connection: close Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>404 Not Found</TITLE> </HEAD><BODY> <H1>Not Found</H1> The requested URL $page was not found on this server.<P> </BODY></HTML> EOF } last; }; } kill(9, $pid) && print "killed pid: $pid\n"; } while () { $host = $server->accept(); if ($pid = fork()) { spawn_srvr(); } $host->close; }

In reply to RE: How do I write a simple webserver by Zarathustra
in thread How do I write a simple webserver by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.