Serving files with one line of code

Posted by Afsal on 14-Jul-2023

Hi Pythonistas!

Today we will learn a simple technique to serve file in a directory with a single command.

This is very useful to serve file on local network. One use case is share images or videos on machine to every one on your network. Python has a standard library called http.server. Using that we can make our machine a server. 

first change the directory to directory you wish to serve

cd /home/afsal/testdir

Then run 

python3 -m http.server

This will starts the server on 8000 port. If you want to change port

python3 -m http.server 8080

This will starts the server on 8080

Then open browser open http://localhost:8000/ or http://<your ip>:<port>. This will show you you a page will list all the files in it.

This basic server will allow you to browse and download the files located in the specified directory. Note that it does not provide any authentication or advanced functionality. If you require additional features, you may need to explore more advanced libraries or frameworks such as Flask or Django.

Please share your valuable suggestions with