Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Perl web server

by Anonymous Monk
on Jul 10, 2004 at 19:34 UTC ( #373391=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Is there any alternative to Stunnix Perl Web Server, I need a free web server to embedded into my program and burn it into CD.

If there is none how could I make one?

Replies are listed 'Best First'.
Re: Perl web server
by EvdB (Deacon) on Jul 10, 2004 at 19:41 UTC
    Try HTTP::Daemon.

    --tidiness is the memory loss of environmental mnemonics

Re: Perl web server
by toma (Vicar) on Jul 10, 2004 at 19:41 UTC
    I use HTTP::Daemon for this sort of thing.

    It should work perfectly the first time! - toma

      Care to suggest how to extend this into a working webserver?

      use HTTP::Daemon; use HTTP::Status; my $d = HTTP::Daemon->new || die; print "Please contact me at: <URL:", $d->url, ">\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r->method eq 'GET' and $r->url->path eq "/xyzzy") { # remember, this is *not* recommended practice :-) $c->send_file_response("/etc/passwd"); } else { $c->send_error(RC_FORBIDDEN) } } $c->close; undef($c); }

      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
        Here's fragment of a little search engine that I wrote. It uses a web interface. It searches an array of data that is retrieved from a file using Storable. The array is searched with regular expressions. In the code posted here, I have left out the routines that generate the HTML and do the searching, leaving just the web server part.

        This is not intended to be a full-featured web server, or even a general-purpose minimal web server. It is a browser UI for a specific application, which is typically only used on a laptop that is disconnected from a network.

        When connected to the network, users access a full-featured database-driven web site instead of this little one. The database behind the full-featured site generates the Storable file used by the little laptop version.

        use Storable qw(retrieve); use HTTP::Daemon; use HTTP::Status; use CGI qw(:standard unescape); $|++; my $d = HTTP::Daemon->new(LocalPort => 8087) || die; print "Point browser to: ", $d->url, "\n"; while (my $c = $d->accept) { my $r = $c->get_request; if ($r->method eq 'GET') { print "Getting content for ",$r->url->path,"\n" if $verbose > 0; if ($r->url->path =~ /\/cdsu/) { my $query = $r->content; my $uri = $r->uri; my $header = $c->send_basic_header; my ($srchpat, $srchres, $srchmsg); if ($uri =~ /srch=([^&]+)/) { $srchpat = $1; $srchpat = unescape($srchpat); $srchmsg = "Search pattern: $srchpat"; $srchpat =~ s/\*/.*/g; $srchpat =~ s/\?/.?/g; $srchres = mpn_search($srchpat); } else { $srchmsg= "Please enter an MPN search value, eg *74*74*"; $srchres= ""; } my $response= gen_content($header, $srchres, $d->url, $srchmsg); print "Query: $query\n", "URI: $uri\n", "Search Pattern: $srchpat\n" if $verbose > 0; $c->send_response($response); } elsif ($r->url->path =~ "/(.*?).gif") { $c->send_file_response("$1.gif"); } else { my $header= $c->send_basic_header; my $response= gen_help($header, $d->url); $c->send_basic_header; $c->send_response($response); } } else { $c->send_error(RC_FORBIDDEN) } $c->close; undef($c); }
        This style of code can get out of hand quickly, because it tends to turn into a large case statement.

        I have a different program that is a little more complex that also uses HTTP::Daemon. In the other program, I used a hash of anonymous subs to generate the content. The key to the hash is a fragment of the URL. It is extracted from the URL with a regular expression. This eliminates the need for a large case statement.

        It should work perfectly the first time! - toma
Re: Perl web server
by tachyon (Chancellor) on Jul 10, 2004 at 22:32 UTC
Re: Perl web server
by strat (Canon) on Jul 11, 2004 at 09:21 UTC

    I very often use POE... you will find some useful code examples at POE-Cookbook -> Web

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

Re: Perl web server
by FreeBeerReekingMonk (Deacon) on Mar 01, 2016 at 20:38 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://373391]
Approved by EvdB
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2020-08-12 18:40 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (66 votes). Check out past polls.