How to transfer files over a network using Python

I recently had to transfer a couple of files from a desktop computer to my phone. Doing this
is easy when you have a USB cable in hand, unfortunately I didn’t have a USB cable with me when I needed to transfer the files. The desktop that had the files I needed did not have bluetooth or a file sharing application that I was signed into. The only thing my phone had in common with the computer was that they were both connected to the same computer network. Phone over WiFi and PC over Ethernet. http.server, a Python module came to my rescue and allowed me to download the files I needed from the computer into the phone. I will show you how to use this module.

http.server or SimpleHTTPServer in Python 2 is a module for creating a basic web server that serves files in the current directory and below. This module can be invoked directly from the terminal using the -m switch of the interpreter with an optional port number argument:

$ python -m SimpleHTTPServer 8000

for Python 3:

$ python -m http.server 8000

Running that simple command in the terminal will start the Web Server. Sharing files over a network can be risky, so avoid running the web server from a top level directory that contains sensitive files. To start the server, navigate to a folder that contains the files you want to share and run the python -m http.server 8000 command from there. Doing this will make sure that you don’t share more than what you need to.

In order to get the files from a device, you first have to figure out what its IP address is. In Linux and Mac the command to run to get this information is: ifconfig
Output from ifconfig command

In Windows the command is: ipconfig.

After starting the python web server, any network connected device can access the files in the web server’s directory. To get the files, open a web browser in the device you want to copy files to and navigate to the server’s IP address and port.

For example if your server is running from port 8000, the files it serves can be accessed by navigating to in the browser. The screenshots below shows a web browser tab from a mobile device:

Directory list of files

From here, files can be viewed or downloaded the same way you would on the Internet. The Python web server can also be used for sharing work progress with colleagues over the network without leaving your desk.

Thank you for reading!