Simple way to host files

Many a times I have felt the need to quickly transfer files from my Linux laptop to a windows machine or an android phone. I did have android apps like Portal or Sweech to handle wireless file transfers to my phone, but file movement between windows machine was largely at the mercy of USB transfers. I wanted some quick solution for this and surprisingly the solution was already with me all along.

Most Linux installation come with Python3 installed. A quick check will tell you whether you have it on your machine.

$ which python3
/usr/bin/python3

Simple HTTP server is a built-in python module that can be used to launch a lightweight server suitable for running basic web applications and lightweight file server. Simple HTTP server serves all the files located in the folder it is run from.

$ cd ~/Downloads
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

This runs an HTTP server on the default port 8000. To run on a different port provide a new port number as an argument.

$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...

The files can then be accessed via any browser by accessing the location. For other devices in the network, the ip address of the machine can be used to access the files. eg http://192.168.0.10:8080/

Accessing hosted files via browser

I ran these commands on a sample directory on my laptop and was able to access the files via browser. This is a quick and painless solution to sharing files on a home network. Of course it lacks any security features so it is best not to use it for any critical application, but definitely works as a simple hack to be able share files. Imagine this running on a Raspberry Pi and voila you have a simple file server for your home network. The only limitation being you can access the files hosted but wont be able to upload any.

A simple hack for a simple use case. Works for me as I do not intend to implement solutions with any more complexity. I tested it and sure enough it does what it says.

EDIT: I wanted to see if I could add a html form which will allow me to upload file to the hosted server and write some code around this to get it working. Turns out this has already been implemented and I found a working code on github

https://gist.github.com/touilleMan/eb02ea40b93e52604938 has the complete working python script which adds a file upload html and also additional code to process the file upload as a post call. The script starts the simple http server on port 8000.

I wanted this to run on 9999 as I was doing before. And also thought of enhancing the functionality so that a firefox instance is also started which loads the list of files hosted on the http server. I ended up tweaking the above source code to following. It starts a firefox instance with the url of the http server that gets started.

def test(HandlerClass = SimpleHTTPRequestHandler,
         ServerClass = http.server.HTTPServer):
    hostname = socket.gethostname()
    local_ip = socket.gethostbyname(hostname)
    os.system("firefox http://"+local_ip+":9999 &")
    http.server.test(HandlerClass, ServerClass, port=9999, bind='0.0.0.0')

Tweaked the code to identify the IP address of the machine, start a firefox instance in background which opens the url.

Following images will demonstrate how this script works. It allows to choose a file for uploaded, shows success message after upload and on returning back to the file list, the recently uploaded file is visible.

I was able to test this via mobile browser as well. Works great for home network.