Running Simple HTTP Server with Python / Linux / Running Simple HTTP Server with Python

Python has a built-in module to quickly start a simple HTTP server. To run it, use the command:

For Python 3.x version:

# python3 -m http.server

For Python 2.x:

python -m SimpleHTTPServer

By default, this will start an HTTP server on port 8000. This HTTP server will share the contents of the current directory.

Most often, I use this method to quickly get files from a Linux host to another device. For example, I need to download a specific log file from the /var/log directory.

To do this, go to the specified directory:

$ cd /var/log

And start the python HTTP server:

$ python3 -m http.server 8080

start python3 m http.server

If this port is blocked by a firewall, you must first temporarily open it:

$ sudo firewall-cmd --add-port=8080/tcp

Or in Ubuntu:

$ sudo ufw allow 8080

Now open a browser on another device, go to URL, and download the necessary files.

share linux files via python http

The HTTP server writes logs of all HTTP GET queries to the console.

Then return to the Linux console and stop the python web server process by pressing Ctrl+C.

python http server log

This trick can also be used to transfer files from WSL to Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *