Simple file server to serve current directory

51,137

Solution 1

python3 -m http.server

or if you don't want to use the default port 8000

python3 -m http.server 3333

or if you want to allow connections from localhost only

python3 -m http.server --bind 127.0.0.1

See the docs.


The equivalent Python 2 commands are

python -m SimpleHTTPServer

python -m SimpleHTTPServer 3333

There is no --bind option.

See the Python 2 docs.

Solution 2

For Node, there's http-server:

$ npm install -g http-server
$ http-server Downloads -a localhost -p 8080
Starting up http-server, serving Downloads on port: 8080
Hit CTRL-C to stop the server

Python has:

  • Python 3: python -m http.server --bind 127.0.0.1 8080
  • Python 2: python -m SimpleHTTPServer 8080

Note that Python 2 has no --bind option, so it will allow all connections (not just from localhost).

Solution 3

There is the Perl app App::HTTPThis or I have often used a tiny Mojolicious server to do this. See my blog post from a while back.

Make a file called say server.pl. Put this in it.

#!/usr/bin/env perl

use Mojolicious::Lite;

use Cwd;
app->static->paths->[0] = getcwd;

any '/' => sub {
  shift->render_static('index.html');
};

app->start;

Install Mojolicious: curl get.mojolicio.us | sh and then run morbo server.pl.

Should work, and you can tweak the script if you need to.

Solution 4

Using Twisted Web:

twistd --pidfile= -n web --path .  --port 8080

--pidfile= disables the PID file. Without it a twistd.pid file will be created in the current directory. You can also use --pidfile ''.

Share:
51,137

Related videos on Youtube

Reactormonk
Author by

Reactormonk

Updated on November 02, 2020

Comments

  • Reactormonk
    Reactormonk over 3 years

    I'm looking for a dead simple bin that I can launch up in the shell and have it serve the current directory (preferably not ..), with maybe a -p for specifying port. As it should be a development server, it should by default allow connections from localhost only, maybe with an option to specify otherwise. The simpler, the better.

    Not sure which tags to use here.

    • Kyle Maxwell
      Kyle Maxwell about 11 years
      You should probably post this over on serverfault.com instead.
    • Alec
      Alec about 11 years
    • vossad01
      vossad01 almost 7 years
      Questions about "software tools commonly used by programmers" are on-topic. When you do web development (and even other types of development nowadays) you end up needing to access local files via HTTP. Perhaps this is still off-topic because it is asking for a tool recommendation, but I disagree on the closure reason given.
  • Reactormonk
    Reactormonk about 11 years
    I don't like piping into a shell.
  • Joel Berger
    Joel Berger about 11 years
    Ok then install via cpan: cpan -i Mojolicious or via apt (though it might be a little old. This is just a quick install script :-)
  • alexm
    alexm about 8 years
    The one-liner version of that Mojolicious application would be: perl -Mojo -E 'a->static->paths(["."]);a->start' daemon
  • nurettin
    nurettin over 5 years
    I'd like to see number of folders and directories as well
  • User9102d82
    User9102d82 over 5 years
    does't work. I can't see any directories. I get an error page with mojo. what am i missing ?
  • Adverbly
    Adverbly over 4 years
    There is also a directory argument in case you don't want the current dir: python3 -m http.server -d /path/to/web/dir