Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by will_ (Beadle)
on Mar 21, 2016 at 11:32 UTC ( #1158429=note: print w/replies, xml ) Need Help??


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

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?

  • Comment on Re^3: Simple http server one-liner for some static files?

Replies are listed 'Best First'.
Re^4: Simple http server one-liner for some static files?
by bcarroll (Pilgrim) on Oct 01, 2016 at 00:27 UTC
    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...

      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". ;-)
Re^4: Simple http server one-liner for some static files?
by Anonymous Monk on Mar 21, 2016 at 11:51 UTC

    Perl does not have a simple HTTP server as part of its core modules. You will always have to download something from CPAN, which will always involve a bit more effort than your Python one-liner.

    So what is your point exactly? ... or are you just venting?

    If you want to use Perl to its fullest potential, you really should take the time to get to know how to use CPAN properly, and then yes, it will become relatively "easy".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2019-08-22 00:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?