333

Python's http.server (or SimpleHTTPServer for Python 2) is a great way of serve the contents of the current directory from the command line:

python -m http.server

However, as far as web servers go, it's very slooooow...

It behaves as though it's single threaded, and occasionally causes timeout errors when loading JavaScript AMD modules using RequireJS. It can take five to ten seconds to load a simple page with no images.

What's a faster alternative that is just as convenient?

10
  • 18
    This thread just restored my sanity. I had been using SimpleHTTPServer and getting random errors with RequireJS that were driving me nuts! node's http-server is working like a charm. Thanks! Nov 15, 2012 at 21:41
  • 4
    @ChrisF, I have edited the question according to this guideline to more grammatically reflect the fact that this is actually an answerable question. I have explained the problem I faced (namely, timeouts and wasted time), and I couldn't list what I had done to address the problem because I didn't know of any alternatives. I don't think this question fits the "What's your favourite ___" shape, as the criteria are clearly defined. Different visitors may find different answers more useful, and answers may not suit the criteria given. Aug 4, 2014 at 19:53
  • 27
    Love how SO has a habit of closing people's favourite questions… Oct 24, 2014 at 19:26
  • 4
    This question is not only useful, it also does not match the description for which it is being closed. At least the research has already been done... Oct 28, 2014 at 18:12
  • 3
    Another option, if you want to serve up a git repo, is git instaweb. Jan 21, 2015 at 15:31

15 Answers 15

421

http-server for node.js is very convenient, and is a lot faster than Python's SimpleHTTPServer. This is primarily because it uses asynchronous IO for concurrent handling of requests, instead of serialising requests.

Installation

Install node.js if you haven't already. Then use the node package manager (npm) to install the package, using the -g option to install globally. If you're on Windows you'll need a prompt with administrator permissions, and on Linux/OSX you'll want to sudo the command:

npm install http-server -g

This will download any required dependencies and install http-server.

Use

Now, from any directory, you can type:

http-server [path] [options]

Path is optional, defaulting to ./public if it exists, otherwise ./.

Options are [defaults]:

  • -p The port number to listen on [8080]
  • -a The host address to bind to [localhost]
  • -i Display directory index pages [True]
  • -s or --silent Silent mode won't log to the console
  • -h or --help Displays help message and exits

So to serve the current directory on port 8000, type:

http-server -p 8000
21
  • 12
    This is awesome!! Thank you for pointing it out. It's great for testing streaming audio/video which is something the python server doesn't seem to handle well at all.
    – gman
    Nov 28, 2012 at 15:44
  • 1
    @Salmonface, did you definitely use the -g option, and did you make sure that no errors were printed during the installation? That error just means it cannot be found after the install, which seems unlikely if things went well. What platform are you on? Run a find command across your drive to find a file with name http-server. I've used this successfully on a few different Linux distros and Windows versions. Dec 27, 2013 at 14:53
  • 2
    I just want to confirm that using this solution improved my page load time from 20s to 2s!
    – 0leg
    Dec 10, 2015 at 10:57
  • 1
    Well, at first it was an improvement over the python SimpleHTTPServer until I ran out of memory. http-server consumes a huge amount of memory for large files. The myserver.go proposal is fast an consumes just about 2 MB instead of 5 GB Memory, before starting to swap and getting really slow.
    – daniel
    Apr 14, 2016 at 21:03
  • 2
    So as of Nov 2019 it looks as though http-server has been broken for windows users for several months. Many of its dependencies are way out of date. One of them, ecstatic, is now abandoned so it's not clear when or if it will be fixed. I looked into fixing myself but it's also not clear if the devs will take PRs. So, I wrote my own replacement.
    – gman
    Nov 4, 2019 at 9:26
117
+50

I recommend: Twisted (http://twistedmatrix.com)

an event-driven networking engine written in Python and licensed under the open source MIT license.

It's cross-platform and was preinstalled on OS X 10.5 to 10.12. Amongst other things you can start up a simple web server in the current directory with:

twistd -no web --path=.

Details

Explanation of Options (see twistd --help for more):

-n, --nodaemon       don't daemonize, don't use default umask of 0077
-o, --no_save        do not save state on shutdown

"web" is a Command that runs a simple web server on top of the Twisted async engine. It also accepts command line options (after the "web" command - see twistd web --help for more):

  --path=             <path> is either a specific file or a directory to be
                      set as the root of the web server. Use this if you
                      have a directory full of HTML, cgi, php3, epy, or rpy
                      files or any other files that you want to be served up
                      raw.

There are also a bunch of other commands such as:

conch            A Conch SSH service.
dns              A domain name server.
ftp              An FTP server.
inetd            An inetd(8) replacement.
mail             An email service
... etc

Installation

Ubuntu

sudo apt-get install python-twisted-web (or python-twisted for the full engine)

Mac OS-X (comes preinstalled on 10.5 - 10.12, or is available in MacPorts and through Pip)

sudo port install py-twisted

Windows

installer available for download at http://twistedmatrix.com/

HTTPS

Twisted can also utilise security certificates to encrypt the connection. Use this with your existing --path and --port (for plain HTTP) options.

twistd -no web -c cert.pem -k privkey.pem --https=4433
14
  • 6
    Unless you already have node.js set up, I found this to be the most convenient. Thanks for sharing!
    – Chris J
    Mar 21, 2013 at 9:55
  • 4
    On Ubuntu, you have to sudo apt-get install python-twisted-web first. (Thanks for this answer, it's very convenient!)
    – nkorth
    Jul 25, 2013 at 15:55
  • 2
    One special advantage of twisted one line server, it support resumable downloads (byte range support), and that is a must have feature when you are downloading large files.
    – Pankaj
    Nov 10, 2013 at 6:38
  • 8
    using node did not stream video/audio properly for me, using twistd works great though!
    – dizy
    Nov 19, 2013 at 4:41
  • 2
    You can configure ports and get other options using twistd --help and twistd web --help. Took me a while to figure that out. Dec 3, 2013 at 20:02
35

1.0 includes a http server & util for serving files with a few lines of code.

package main

import (
    "fmt"; "log"; "net/http"
)

func main() {
    fmt.Println("Serving files in the current directory on port 8080")
    http.Handle("/", http.FileServer(http.Dir(".")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Run this source using go run myserver.go or to build an executable go build myserver.go

1
  • Great answer. This runs faster than SimpleHTTPServer and nodejs solution. :) Is there any way I can add username and password to the download?
    – Ajax
    May 22, 2016 at 21:47
22

Try webfs, it's tiny and doesn't depend on having a platform like node.js or python installed.

4
  • 1
    looks like you have to compile it? Didn't see any binaries for download. Apr 4, 2014 at 13:40
  • 3
    yes, unless your distro has it. Debian and Ubuntu have it: apt-get install webfs
    – Hudon
    Apr 4, 2014 at 17:41
  • 4
    I did a brew install webfs on my Mac which resulted in 🍺 /usr/local/Cellar/webfs/1.21: 5 files, 96K, built in 15 seconds. Afterwards I could just say webfsd -F -p 3003 -r resources/public/ -f index.html to achieve the same as twistd -no web -p 3003 --path=resources/public/. It's a bit wordy so not obvious to remember but good to know as an alternative to twistd or SimpleHTTPServer.
    – onetom
    Dec 20, 2014 at 20:18
  • Much better than the other solutions. It's very small, very fast and provided as an OS package. It also supports SSL and can run as a system daemon.
    – Federico
    Nov 7, 2019 at 15:04
15

If you use Mercurial, you can use the built in HTTP server. In the folder you wish to serve up:

hg serve

From the docs:

export the repository via HTTP

    Start a local HTTP repository browser and pull server.

    By default, the server logs accesses to stdout and errors to
    stderr. Use the "-A" and "-E" options to log to files.

options:

 -A --accesslog       name of access log file to write to
 -d --daemon          run server in background
    --daemon-pipefds  used internally by daemon mode
 -E --errorlog        name of error log file to write to
 -p --port            port to listen on (default: 8000)
 -a --address         address to listen on (default: all interfaces)
    --prefix          prefix path to serve from (default: server root)
 -n --name            name to show in web pages (default: working dir)
    --webdir-conf     name of the webdir config file (serve more than one repo)
    --pid-file        name of file to write process ID to
    --stdio           for remote clients
 -t --templates       web templates to use
    --style           template style to use
 -6 --ipv6            use IPv6 in addition to IPv4
    --certificate     SSL certificate file

use "hg -v help serve" to show global options
12

Here's another. It's a Chrome Extension

Once installed you can run it by creating a new tab in Chrome and clicking the apps button near the top left

It has a simple gui. Click choose folder, then click the http://127.0.0.1:8887 link

enter image description here

https://www.youtube.com/watch?v=AK6swHiPtew

0
10

I found python -m http.server unreliable—some responses would take seconds.

Now I use a server called Ran https://github.com/m3ng9i/ran

Ran: a simple static web server written in Go

9

Also consider devd a small webserver written in go. Binaries for many platforms are available here.

devd -ol path/to/files/to/serve

It's small, fast, and provides some interesting optional features like live-reloading when your files change.

5

If you have PHP installed you could use the builtin server.

php -S 0:8080
2
  • Much better solution than Python/Node. May 20, 2021 at 16:49
  • For me, php -S localhost:8080
    – ggorlen
    Aug 28, 2021 at 18:18
4

give polpetta a try ...

npm install -g polpetta

then you can

polpetta ~/folder

and you are ready to go :-)

1
3

Using Servez as a server

  1. Download Servez
  2. Install It, Run it
  3. Choose the folder to serve
  4. Pick "Start"
  5. Go to http://localhost:8080 or pick "Launch Browser"

servez

Note: I threw this together because Web Server for Chrome is going away since Chrome is removing support for apps and because I support art students who have zero experience with the command line

1
  • I am voting up for running WebGL examples Apr 29, 2019 at 15:58
2

Yet another node based simple command line server

https://github.com/greggman/servez-cli

Written partly in response to http-server having issues, particularly on windows.

installation

Install node.js then

npm install -g servez

usage

servez [options] [path]

With no path it serves the current folder.

By default it serves index.html for folder paths if it exists. It serves a directory listing for folders otherwise. It also serves CORS headers. You can optionally turn on basic authentication with --username=somename --password=somepass and you can serve https.

1

I like live-server. It is fast and has a nice live reload feature, which is very convenient during developpement.

Usage is very simple:

cd ~/Sites/
live-server

By default it creates a server with IP 127.0.0.1 and port 8080.

http://127.0.0.1:8080/

If port 8080 is not free, it uses another port:

http://127.0.0.1:52749/

http://127.0.0.1:52858/

If you need to see the web server on other machines in your local network, you can check what is your IP and use:

live-server --host=192.168.1.121

And here is a script that automatically grab the IP address of the default interface. It works on macOS only.

If you put it in .bash_profile, the live-server command will automatically launch the server with the correct IP.

# **
# Get IP address of default interface
# *
function getIPofDefaultInterface()
{
    local  __resultvar=$1

    # Get default route interface
    if=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
    if [ -n "$if" ]; then
            # Get IP of the default route interface
            local __IP=$( ipconfig getifaddr $if )
            eval $__resultvar="'$__IP'"
    else
        # Echo "No default route found"
        eval $__resultvar="'0.0.0.0'"
    fi
}

alias getIP='getIPofDefaultInterface IP; echo $IP'

# **
# live-server
# https://www.npmjs.com/package/live-server
# *
alias live-server='getIPofDefaultInterface IP && live-server --host=$IP'
0

I've been using filebrowser for the past couple of years and it is the best alternative I have found.

Features I love about it:

  • Cross-platform: It supports Linux, MacOs and Windows (+). It also supports docker (+).
  • Downloading stuff is a breeze. It can automatically convert a folder into zip, tar.gz and etc. for transferring folders.
  • You can file or folder access to every use.

enter image description here

2
  • 1
    Does this serve up files directly over http in a way that can be used by web developers? It looks to be only a file browser. Jul 18, 2022 at 1:54
  • Yes, you can access it via REST API. See here and here. Jul 18, 2022 at 9:45
0

miniserve

miniserve

  • cross platform
  • single executable file
  • lightweight ( < 2MB for Windows / Linux x86 )
miniserve . --index index.html

Vercel serve (node.js)

If node.js is installed, I recommended vercel serve

# for node.js >= v14
npm install --global serve

# for node.js <= v13
npm install --global serve@13
serve

http-server (node.js)

http-server latest version does not work ( see issue ), but v13 works

npm install --global http-server@13
http-server

Not the answer you're looking for? Browse other questions tagged or ask your own question.