Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Anonymous Monk
on Mar 02, 2016 at 15:20 UTC ( #1156677=note: print w/replies, xml ) Need Help??


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

... that only uses core modules.

Why? Yes, even you can use CPAN

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

Replies are listed 'Best First'.
Re^3: Simple http server one-liner for some static files?
by will_ (Beadle) on Mar 21, 2016 at 11:32 UTC

    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?

      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". ;-)

      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://1156677]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2019-08-21 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?