Linux for Network Engineers: A one-line HTTP server

By April 8, 2020Linux

There are multiple ways to get a file from a remote Linux host. To name a few: scp, sftp, tftp, http/https… And you could probably add more in the list. Depending on your requirements in terms of security, ease of use, availability, and set-up effort you have to decide what is the best option for you.

When it comes to getting a file within a LAN where you don’t have to worry about security, the easiest solution for me is, hands down, setting up a simple HTTP server with Python. Given the fact that Python is prevalent you can do that with the following commands for Python 2:

python -m SimpleHTTPSever 

And for Python 3:

python -m http.server

Of course, the tradeoff is that it offers absolutely no security or protection of your content. I would never use this as a public facing server if you are worried about unauthorized users accessing it or if you are looking for a high availability server.

By default this will set up an HTTP server on port 8000 with the current working directory as the root directory. If you need a custom port, e.g. 8080, you just have to add it at the end as follows python -m SimpleHTTPSever 8080 and python -m http.server 8080. Keep in mind that if you want to use a port lower than 1024 you’d have to run the command as root, and in most cases this is not necessary. After all, the purpose here is to download a file and then stop the server.

That’s it! Now point your browser to the <IP>:<port> and you will see the directory contents. Here is what it looks like on my browser. I can click on the file I want to download and then I can kill the HTTP server with Ctrl+c

On another Linux host, I can transfer the file with wget:

netbeez $ wget 172.31.0.69:8000/logs.txt
--2020-04-07 14:47:01--  http://172.31.0.69:8000/logs.txt
Connecting to 172.31.0.69:8000... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6 [text/plain]
Saving to: ‘logs.txt’

logs.txt                100%[==============================>]       6  --.-KB/s    in 0s

2020-04-07 14:47:01 (197 KB/s) - ‘logs.txt’ saved [6/6]

Or with curl:

curl 172.31.0.69:8000/logs.txt --output logs.txt
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100     6  100     6    0     0     25      0 --:--:-- --:--:-- --:--:--    26

I use this very often when I need a quick and dirty way to transfer a file between two hosts. It’s particularly useful when you want to download a file from a Linux host to a machine that doesn’t have a command line or other utilities such as sftp or scp (e.g. Windows). It’s also very convenient when you want to share ad hoc a file with an unskilled user that can’t use any other software. Just send them the link and they can click and download the file they need.