Monolune

Serving a directory through HTTP

During development, serving static files from a folder through HTTP is a good way of knowing how your website will look in production when doing HTML, Javascript, CSS development. Serving static files through the HTTP protocol instead of viewing the file directly will allow relative and absolute links to work properly. If you already have python installed on your system, you can already use it to serve static files without needing further installation of software. This article will present two methods of serving a directory. Which method you choose will depend on your specific needs.

Built-in Python solution

If you already have python installed, the easiest way to serve files from a folder is by running:

python -m SimpleHTTPServer

Doing so will serve your present working directory on http://localhost:8000, which you can access using a web browser. If you prefer a port number other than the default port 8000, you can specify the port using:

python -m SimpleHTTPServer <port-number>

Replace <port-number> with the desired port number (e.g. 8080, 9000, etc.).

If you have python3, the command is:

python3 -m http.server

Doing so will also serve the present working directory on port 8000. To see all the available command line options, python3 -m http.server --help. The help documents options for changing the address and port through which the directory is served.

Twistd solution

The python and python3 solutions presented above may be convenient, but they are known to be slow. For some uses, the slow speed is noticeable, and may may development difficult. If you need something faster, you can install twistd using pip install twistd, or if you are using Debian or Ubuntu, sudo apt-get install python-twistd. Once you have installed twistd, you can serve the directory using:

twistd --nodaemon --no_save web --path=. --port=8000

Note: You may encounter problems with specifying the port number when using more recent version of twistd (i.e. ValueError: Unknown endpoint type: '9000'). In that case, you can run twistd --nodaemon --no_save web --path=. --port='tcp:port=8000' instead.

Conclusion

Those are two of the ways you can serve static files in a directory over HTTP for development purposes. The solutions presented are not suitable for production purposes. For that, you can use NGINX or the Apache web server. Instead of remembering and typing out the commands every time you wish to serve files, you can define an alias or shell function in your .bashrc or .zshrc. For example:

function http-serve {
    python -m SimpleHTTPServer "${1:-8000}"
}

function http-serve {
    python3 -m http.server "${1:-8000}"
}

function http-serve {
    twistd --nodaemon --no_save web --path=. --port="${1:-8000}"
}

Take your pick of one of the shell functions. Putting one of them in your shell configuration file allows you to run http-serve <port-number>. If the port number argument is not provided, the port number defaults to 8000. I hope this article has helped.