Transferring Files Using Python’s Built-In HTTP Server | Complete Tutorial 💯

The need to transfer files over a network is one that arises often. GNU/Linux systems support multiple protocols and tools for doing so, some of which are designed for somewhat permanent file sharing (such as SMB, AFP, and NFS), while others such as Secure Copy (SCP) are used for quick manual and scripted file transfers. Among these is the HyperText Transfer Protocol (HTTP), the versatile and ubiquitous protocol on which the World Wide Web relies.

Python, which is included by default in most Linux distributions, provides simple HTTP servers through the “SimpleHTTPServer” and “http.server” modules. The former is found in the Python 2 Standard Library, while the latter is included in Python 3. These lightweight HTTP servers require no separate installation and can be started instantly with a single command.

Installing Python

Your system most likely includes at least one Python version, but if that’s not the case, install Python 3 using your native package manager.

For example, on Debian and Ubuntu:

sudo apt update sudo apt install -y python3

Starting the HTTP Server

Take note of the IP address used by the sending machine.

ip a s

image

Find out which Python version is installed with the following commands:

python --version
python3 --version

On the same machine, change your working directory to the one containing the files you’re transferring. Be aware of the fact that the entire contents of your current working directory may be accessible to anyone on your network (or the Internet if the sending machine has a public IP address), while the Python HTTP server is running.

cd /path/to/files/

For example:

cd /home/user/Documents/

You can now start the HTTP server. For Python 2.x, use the SimpleHTTPServer module:

python -m SimpleHTTPServer

Or http.server in the case of Python 3.x:

python3 -m http.server

Both variations listen on port 8000 by default, though you can explicitly specify a different port number after the module name.

python -m SimpleHTTPServer [port]
python3 -m http.server [port]

Note: root privileges are required if you choose a port under 1024.

Downloading Your Files

On the receiving machine, you can use any HTTP client to download your files. If you’re using a graphical environment, a browser is often more convenient than command-line utilities. Simply browse to http://IP_ADDRESS:8000, where “IP_ADDRESS” is the IP address of the sending computer, and click on the desired files to download them.

image

Alternatively, you can use Wget to fetch your files. You should already have one or both of them installed. If neither are, we suggest installing Wget, as it is more user-friendly and supports downloading whole directories.

For Debian and Ubuntu:

sudo apt install wget

For RHEL and CentOS 6/7:

sudo yum install wget

For Fedora and RHEL/CentOS 8:

sudo dnf install wget

Using Wget

To download a single file with Wget, simply invoke Wget followed by the URL of the file you want to download.

wget http://IP_ADDRESS:8000/filename

image

You can also use Wget to recursively download the whole directory by adding the -r command-line flag.

wget -r http://IP_ADDRESS:8000/

image

Using cURL

By default, curl tries to print file contents to your terminal. So to save the file instead, specify a filename with the -o flag.

curl http://IP_ADDRESS:8000/filename -o filename

Conclusion

The HTTP functionality in Python’s standard library supplies a basic yet fast and convenient way of transferring files, perfect for some scenarios. But keep in mind that because this is plain HTTP with neither encryption nor authentication, you should be careful to not expose sensitive files.

ENJOY & HAPPY LEARNING! :+1:

18 Likes