What is a faster alternative to Python's http.server (or SimpleHTTPServer)?

Command LineHttpserverCommand Line-ToolSimplehttpserver

Command Line Problem Overview


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?

Command Line Solutions


Solution 1 - Command Line

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

Solution 2 - Command Line

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

Solution 3 - Command Line

[tag:go] 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

Solution 4 - Command Line

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

[1]: http://linux.bytesex.org/misc/webfs.html "webfs"

Solution 5 - Command Line

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

Solution 6 - Command Line

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

Solution 7 - Command Line

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

Solution 8 - Command Line

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.

Solution 9 - Command Line

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

php -S 0:8080

Solution 10 - Command Line

give polpetta a try ...

> npm install -g polpetta

then you can

> polpetta ~/folder

and you are ready to go :-)

Solution 11 - Command Line

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

Solution 12 - Command Line

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.

Solution 13 - Command Line

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrew NoakesView Question on Stackoverflow
Solution 1 - Command LineDrew NoakesView Answer on Stackoverflow
Solution 2 - Command LinePeter GibsonView Answer on Stackoverflow
Solution 3 - Command Linepd40View Answer on Stackoverflow
Solution 4 - Command LineHudonView Answer on Stackoverflow
Solution 5 - Command LineDrew NoakesView Answer on Stackoverflow
Solution 6 - Command LinegmanView Answer on Stackoverflow
Solution 7 - Command LineColonel PanicView Answer on Stackoverflow
Solution 8 - Command LinegmanView Answer on Stackoverflow
Solution 9 - Command Lineuser1461607View Answer on Stackoverflow
Solution 10 - Command LineAndrea GiammarchiView Answer on Stackoverflow
Solution 11 - Command LinegmanView Answer on Stackoverflow
Solution 12 - Command LinegmanView Answer on Stackoverflow
Solution 13 - Command LinenicoView Answer on Stackoverflow