Simplest way of serving local files over HTTP?

I've always admired Python's way of serving local files by simply doing:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

And I've been wondering if there's a way to achieve the same in Perl without requiring lots of dependencies from CPAN. This morning I just came out with this:

$ perl -Mojo -E 'a->static->paths(["."]);a->start' daemon
Server available at http://127.0.0.1:3000

It shows the file content, though it doesn't show the directory listing.

Is there any simpler way?

5 Comments

According to this PerlMonks node you can use "http_this", which is this code:

plackup -MPlack::App::Directory -e 'Plack::App::Directory->new->to_app'

Though even that doesn't start with Perl. The web has also yielded

perl -MMojolicious::Lite -MCwd -e 'app->static->paths->[0]=getcwd; app->start' daemon -l http://*:8000

perl -Mojo -MCwd -E"app->static->paths->[0]=getcwd; app->start" daemon -l 'https://*:8000'

perl -MHTTP::Server::Brick -e 'HTTP::Server::Brick->new(fork=>1)->mount(qw(/ .))->start'

perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io($1) if /^GET \/(.*) / })'
# Can even teach that one to do executables!
perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'

I've been using http_this for a long time. It's easy to install if you're used to CPAN and just a simple command without mandatory arguments. Much better than any Perl or Python code to remember and type in each time.

You can check out my own mccs. You can serve cwd with just:

mccs
But no directory listing (at this time)

Is it worth mentioning X-Sendfile ? It is used if your code has decided to serve a static file. There are several ways of using X-Sendfile from Perl.

http://search.cpan.org/dist/Catalyst-Plugin-XSendFile/lib/Catalyst/Plugin/XSendFile.pm

http://www.perlmonks.org/?node_id=1000255
Basically you add a response header then Apache or lighty serves the file for you. That is much more efficient than serving it from Perl.
Cheers -- Rick

Leave a comment

About Alex Muntada

user-pic I love Perl.