Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^4: Simple http server one-liner for some static files?

by bcarroll (Pilgrim)
on Oct 01, 2016 at 00:27 UTC ( #1173043=note: print w/replies, xml ) Need Help??


in reply to Re^3: Simple http server one-liner for some static files?
in thread Simple http server one-liner for some static files?

How about this approach:
  • Create a text file in a directory in your shell's PATH named SimpleHTTPServer.pl
  • Paste the following code into the SimpleHTTPServer.pl text file
    #!/usr/bin/env perl use HTTP::Daemon; my $port = $ARGV[0] || 8000; my $d = HTTP::Daemon->new(LocalPort => $port) or die $!; print "SimpleHTTPServer.pl listening at: ", $d->url, "\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { $c->send_file_response(".".$r->url->path); } }
    If on a UNIX/Linux platform make sure to chmod +x SimpleHTTPServer.pl
  • cd to the directory of your HTML content
  • execute SimpleHTTPServer.pl to create a webserver on port 8000
    or
    execute SimpleHTTPServer.pl PORTNUMBER to create a webserver on port number of your choice

The implementation (a perl script) is not a one liner, but the execution (calling the perl script) is...

Replies are listed 'Best First'.
Re^5: Simple http server one-liner for some static files?
by afoken (Canon) on Oct 01, 2016 at 07:14 UTC
    The implementation (a perl script) is not a one liner, but the execution (calling the perl script) is.

    If the OP insists, (s)he could make it a one-liner:

    perl -MHTTP::Daemon -E '$d=HTTP::Daemon->new(LocalPort=>$ARGV[0]||8000 +);say$d->url;while(my$c=$d->accept){while(my$r=$c->get_request){$c->s +end_file_response(".",$r->url->path)}}'

    Yes, this is hard to read, hard to remember, and generally a stupid idea. (And yes, I'm kind of violating my own rule.) The clean way is to use a script.

    Also, writing a module optimized for using in a one-liner isn't that hard:

    package HTTP::here; use strict; use warnings use Exporter qw( import ); use Whatever::Webserver::you::like; our @EXPORT=qw( serve ); sub serve { my $port=shift; $port||=8080; Whatever::Webserver::you::like->new(port => $port)->run(); } 1;

    Using it is as easy:

    perl -MHTTP::here -e serve

    For a non-default port:

    perl -MHTTP::here -e 'serve(8000)'

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1173043]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-08-24 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?