Serve Your Current Directory With Python and HTTP
Tags: commandline, python, http, tips
Published: 2018-10-22 01:38:09

---
title: Serve Your Current Directory With Python and HTTP
published: true
description: "Short post showing how to run an HTTP server to serve your current working directory."
tags: ["command line", "python", "HTTP", "tips"]
canonical_url: https://blog.nicolasmesa.co/posts/2018/09/serve-your-current-directory-with-python-and-http/
---

Cross-posted from my personal blog [https://blog.nicolasmesa.co](https://blog.nicolasmesa.co).

This is going to be a short post showing how to run an HTTP server to serve your current working directory.

## TLDR

For Python 3 run:

```console
python3 -m http.server
```

For Python 2 run:

```console
python -m SimpleHTTPServer
```

`.bashrc` alias:

```bash
alias serve="python3 -m http.server"
```

## Explanation

Sometimes at work, I’ve had the need to spin up a quick HTTP server to serve my current working directory. I usually need this for two use cases:

1. Share a quick link to have people download something from my computer.
2. I have an `index.html` file in another computer along with a bunch of static resources and don’t feel like copying the whole folder to be able to see the page in my browser. This use case happens a lot when I’m running unit tests on another computer and need to look at coverage reports (which are rendered in HTML).

It turns out that serving your current directory with an HTTP server is easy. You only need `python`.

## Setup

Before we get started, let’s create some sample files.

```console
nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file1 > file1.txt
nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file2 > file2.txt
nmesa@desktop-nicolas:~/demos/serve-cwd$ mkdir dir1 dir2
nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file3 > dir1/file3.txt
nmesa@desktop-nicolas:~/demos/serve-cwd$ echo file4 > dir2/file4.txt
nmesa@desktop-nicolas:~/demos/serve-cwd$ tree
.
├── dir1
│   └── file3.txt
├── dir2
│   └── file4.txt
├── file1.txt
└── file2.txt

2 directories, 4 files
```

We created two files and two directories in the root, and one file in each of the directories.

## Find out your python version

The command to spin up the HTTP server varies depending on the version of python that you have. Run `python --version` to get your current version of `python`.


```console
nmesa@desktop-nicolas:~/demos/serve-cwd$ python --version
Python 3.5.2
```

If you see `Python 3.x.x`, use the Python 3 command. If you see `Python 2.x.x`, use the Python 2 command.

In this case, I’m using Python 3.

## Python 3 command


```console
nmesa@desktop-nicolas:~/demos/serve-cwd$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...
```

## Python 2 command


```console
nmesa@desktop-nicolas:~/demos/serve-cwd$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
```

## Test it out

The previous commands spun up an HTTP server listening on port `8000`. Let’s point our web browser to the server’s IP address on port `8000` (`192.168.0.250:8000` in this case). The page should look like this:

### Root directory

![Sample root directory listing](https://blog.nicolasmesa.co/posts/2018/09/sample_root_directory_listing.png)Sample root directory listing. This is the equivalent of running an `ls` on the directory where the server is running.

### Child directory

Let’s click the `dir1/` link:

![dir1 directory listing](https://blog.nicolasmesa.co/posts/2018/09/dir1_directory_listing.png)`dir1` directory listing.

### File

Let’s click the `file3.txt` link.

![file3.txt contents](https://blog.nicolasmesa.co/posts/2018/09/file3_file_contents.png)`file3.txt` contents. Note that this file rendered in the browser. This is because the HTTP server is smart enough to add a content type to the HTTP response. This becomes handy when you need to serve HTML along with CSS, JS, and images.

## Update (Oct 21, 2018)

I’ve been using this a lot at work to serve test coverage reports. To save some typing, let’s add the following alias to our `.bashrc` (`.bash_profile` in Mac) file:

```bash
alias serve="python3 -m http.server"
```

Let’s test it out by running `serve` from our target directory (note that you will need to run `source ~/.bashrc` or start a new terminal for this to work):

```console
nmesa@desktop-nicolas:~/demos/serve-cwd$ serve
Serving HTTP on 0.0.0.0 port 8000 ...
```

We can also specify a different port:

```console
nmesa@desktop-nicolas:~/demos/serve-cwd$ serve 9000
Serving HTTP on 0.0.0.0 port 9000 ...
```

This command starts the web server in port `9000`.

## Conclusion

Python makes it easy to spin up an HTTP server to share your current working directory. Please don’t forget to kill the server once you’re done and be careful with what you share.

## Links / Further reading

- [Original Post](https://blog.nicolasmesa.co/posts/2018/09/serve-your-current-directory-with-python-and-http/)
- [SimpleHTTPRequestHandler source code (python 3)](https://github.com/python/cpython/blob/3.7/Lib/http/server.py#L627)
- [SimpleHTTPRequestHandler source code (python 2)](https://github.com/python/cpython/blob/2.7/Lib/SimpleHTTPServer.py#L28)
- [alias command](http://www.linfo.org/alias.html)