🇵🇹   Português

Easy to use HTTP server in Linux

Posted on 06-12-2024, by Rodolfo

Wanna transfer files through your LAN rapidly? Just run this short command.

An HTTP server is a web server. It provides pages and files from a server, through the local network or the Internet, to other computers running a web browser or a client program.

The HTTP server, in this case, is provided by a Python module. If you have Python installed on your machine (chances are, since most Linux distros bring it by default) then you can open an HTTP server and share files locally in a really fast way. It's one command only!

The devices (server and clients) must be connected to the same network. You will use the local IP of the server to connect to it. This procedure (considering the way it's detailed in this article) does not work outside a LAN.

Do I have Python installed?

To check if you have Python installed on your Linux box, type the following command in a terminal:

$ python3

This will run the Python interpreter, showing that Python is installed on your system. To get out of the interpreter, hit Ctrl+D or enter quit().

Running the server

  • Open a terminal;
  • Navigate to the folder where the files are (in this example, "MyFiles");
  • Run the provided Python command.

$ cd MyFiles

/MyFiles $ python3 -m http.server

When you finish transfering files, close the server by hitting Ctrl+C.

Explaining the command

  • python3 calls the Python interpreter, which will run your command;
  • The -m is a switch of the Python interpreter. It is used to make the interpreter run a specific module;
  • http.server is the name of the module.

When you enter the command, you will see something like this:

/MyFiles $ python3 -m http.server

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

The server is now running on a terminal in machine 1 and serving the files inside the MyFiles folder. To access the files using machine 2, open a browser in it and enter the local IP of machine 1 and port 8000.

Example: if the local IP of machine 1 is 192.168.0.205, type this in the machine 2 browser: "192.168.0.205:8000".

Address bar in browser

The address bar in the browser (machine 2) showing the server's local IP + port.

Browser in machine nr. 2

The page body showing the files/folders provided by the HTTP server. (CLICK ON THE IMAGE FOR FULL SCALE).

Now you only need to click the files you want and download them to machine nr. 2. If you want to download a folder and don't want to individually click every single file inside it, you must compress the folder before starting the server. After that, you can download the compressed file and decompress it where you prefer to obtain the files.

Machine nr. 1 (the server) can be any device with Python installed, but machine nr. 2 (the client) can be any device connected to the same network as the server, and capable of running a client program or a browser. Python is not necessary in the client. Your phone can do that, for example.

Changing parameters

You can change lots of other parameters too, like the default port that the server listens to:

/MyFiles $ python3 -m http.server 9000

You also have two ways to tell the Python interpreter what directory to use:

Option 1: $ python3 -m http.server -d directoryname

Option 2: $ python3 -m http.server --directory directoryname

(Replace "directoryname" with the directory you want.)

Warning!

As the official Python documentation says: http.server is not recommended for production. It only implements basic security checks.

So use it only for your personal purposes, in your own trusted network.

Consulted sites:

https://developer.mozilla.org/en-US/docs/Learn/Common_questions/Web_mechanics/What_is_a_web_server

https://docs.python.org/3/library/http.server.html