Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Simple http server one-liner for some static files?

by Anonymous Monk
on Oct 13, 2010 at 20:08 UTC ( #865148=perlquestion: print w/replies, xml ) Need Help??

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

Anyone know of a one-liner I can use to serve some static docs via http over some high-numbered port?

In Python I can do:

python -m SimpleHTTPServer $portnum > ~/temp/little-web-server.log 2>& +1 &

(serves documents in the cwd where it was run)

Is there an equivalent for that in Perl?

Replies are listed 'Best First'.
Re: Simple http server one-liner for some static files?
by Your Mother (Archbishop) on Oct 13, 2010 at 20:22 UTC

    There are probably 5 ways to do that. Here's one that supports executables too:

    A Tiny Web Server - Here is how you could write a simplistic web server that works with static and dynamic pages (IO::All):
    perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'

    Only recommended for personal use.

Re: Simple http server one-liner for some static files?
by sundialsvc4 (Abbot) on Oct 14, 2010 at 01:48 UTC

    Yes, Python has quite a few excellent web-server programs ... and Perl has gadzillions.   Just surf over to, search for “HTTP server,” and you’ll see what I mean.

    If you search instead for HTTP::Server, you’ll see some of the most readily-available packages, such as HTTP::Server::Simple and my latest personal favorite, HTTP::Server::Brick.

    Each of these are very comparable to the Python package you cited.   “You got a simple job to do, and you want a simple way to do it.”   Done.

      Better 2 years late, than never. :)

      The only problem with having gadzillions of programs is finding the right one for you. The poster was looking for this:


      which didn't make the first 2 pages of results. Written 3 months earlier than the question, it's just a wrapper around the Plack::App::Directory which was suggested in another thread, but it's simpler, like the python command in a book on D3 that sent me looking for the perl equivalent of python -m SimpleHTTPServer

      perl -e 'print qq(Just another Perl Hacker\n)' # where's the irony switch?
        Joining the "better late than never"-motto, here a (long) one-liner which—in constrast to the answers before—does not require non-core modules:
        perl -MHTTP::Daemon -e '$d = HTTP::Daemon->new(LocalPort => 8000) or +die $!; while ($c = $d->accept) { while ($r = $c->get_request) { +$c->send_file_response(".".$r->url->path) } }'
        Actually this is a flattened and slightly modified version of HTTP::Daemon's synopsis section.

      Hi, yes, I found those -- thank you. Unfortunately, neither of those modules show in their Synopses how to use them to run an http server as a one-liner. If you know how to use either to do that, could you please share?

        It's not difficult. The "-Mxxx" option to perl loads module "xxx" and "-e" lets you supply an expression to evaluate (a command)

        perl -MHTTP::Server::Brick -e '$s=HTTP::Server::Brick->new(port=>8080); $s->mount("/"=>{path=>"/tmp"}); $s->start'

        This serves /tmp and its sub-directories, if you don't want that you'll want wildcard=>0 as well as the path.

        stick with what works
Re: Simple http server one-liner for some static files?
by Your Mother (Archbishop) on Mar 21, 2016 at 12:31 UTC

    And since this dead horse is apparently still being flogged. :P This is the one-liner I tend to use (because there are other middlewares and such that are easy to add and play with).

    plackup -p 5000 -MPlack::App::Directory -e 'Plack::App::Directory->new +({root => q{.}})->to_app'

    I understand that using the tools to get a local Perl install and such are not super easy. They are not too difficult once you get the hang of them. What I would recommend if you decide it's worth the effort: local::lib, cpanm, and perlbrew (and tangentially: Plack for wsgi like Perl -> psgi).

    Update: missing preposition.

Re: Simple http server one-liner for some static files?
by will_ (Scribe) on Mar 02, 2016 at 13:42 UTC
    Still looking for a one-liner that only uses core modules.

        Because I don't want to spend time and energy configuring CPAN, accounting for the company proxy, downloading a module, finding somewhere to put it, worrying about accidentally installing files in the wrong place, making the perl command refer to that location, etc. for every temporary webserver I want to make.

        While you may think all that is "easy", you can't honestly think it's as easy, fast and convenient as typing "python -m SimpleHTTPServer" or any other built-in one-liner?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2021-05-15 17:21 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (150 votes). Check out past polls.