Transferring Files Using Python’s Built-in HTTP Server

Python Http Transfer Featured

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:

Starting the HTTP Server

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

Python Http Transfer Ip Address

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

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.

For example:

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

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

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

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.

Python Http Transfer Browser

Alternatively, you can use Wget or cURL 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:

For RHEL and CentOS 6/7:

For Fedora and RHEL/CentOS 8:

Using Wget

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

Python Http Transfer Wget

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

Python Http Transfer Wget Recursive

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.

Python Http Transfer Curl

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.

One comment

  1. This is really GREAT, never thought of doing this, thanks for writing article

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.