SimpleHTTPServer is a Python module that allows you to establish a web server or serve files in seconds. The main benefit of using Python’s SimpleHTTPServer is that you don’t need to install anything because you already have the Python interpreter. You won’t have to worry about the python interpreter because it’s included by default in practically all Linux distributions.
SimpleHTTPServer can also be used for file sharing. You only need to enable the module in the folder where your shareable files are stored. In this essay, we’ll show you multiple demonstrations utilizing various options.
We’ll learn how to build up a local and straightforward HTTP server with Python in this article. An HTTP server can be convenient for testing Android, PC, or Web apps during development locally. Additionally, you can use it to transfer files between two devices that are linked to the same LAN or WLAN.
Files on your computer vs. files on the internet
We recommend that you open your examples in a browser by double-clicking the HTML file, dragging and dropping it into the browser window, or choosing file> Open… and navigating to the HTML file across the learning area. You can accomplish this in a variety of ways.
A local file is used if the web address path begins with file :/ and then the path of the file on your local hard disk. If you visit one of our GitHub examples (or an example hosted on another remote server), the web URL will begin with http:// or https://, indicating that you received the file via HTTP.
The issue with attempting to test local files
If you open some examples as local files, they will not execute. It could be attributed to several factors, the most common of which are:
- Asynchronous queries are included. If you run the example from a local file, some browsers (including Chrome) will not conduct async queries (Fetching data from the server). It is the case because of security concerns (for more on web security, read Website security).
- They have server-side programming languages like PHP or Python, which require a dedicated server to interpret and deliver the code.
- They also contain additional files. Browsers generally treat requests to load resources using the file:// schema as cross-origin requests. As a result, loading a local file that includes additional local files may result in a CORS issue.
Run the following command to see if Python is installed on your server.
# python –V
# python --version
It will display the version of the Python interpreter you have installed and an error message if it is not. Run the following command at the terminal:
python3 -m http.server
python -m http.server
After successfully enabling SimpleHTTPServer, it will begin delivering files on port 8000. Now, open a web browser and type IP address: port number into the address bar (in our case, it is 192.168.5.67:8000).
Local access to the server
To connect to the server locally, go to http://localhost:8000/.You can see all of the directories in your local storage and the data here. You can also visit an HTML page, which is rendered as you access it by your web browser.
Using a network to connect to the server
Before you start the application, keep in mind that the device on which you ran the preceding commands is known as a Server or a Host, and the second device you’ll use to connect to the server across the network is known as a Client. If you want to connect to the server over the network, make sure that both the server and the client are on the same LAN or WLAN network.
To connect to the server, you’ll need the server’s IP address. You must take the following steps on your Server device to obtain the IP address:
On Windows, use the Windows command prompt to run the following command:
Execute the following command on a Linux, Unix, or macOS terminal:
Take note of the IP address returned by the command above. You will use this IP address in the future. Open your web browser of choice on the Client device. Subsequently, type in the IP address of the first machine (Server device), as well as port 8000:
Congratulations!! You now know how to use Python to run a simple HTTP server.
Note: Because this HTTP server has limited security, you should only use it for development or local file sharing. It is not advised for usage in a production setting.
Changing the port of SimpleHTTPServer
Python’s SimpleHTTPServer serves files and folders over port 8000, but you can use the python command to provide a new port number (here, port 9779) as needed.
python –m SimpleHTTPServer 9779
Serving Files from Various Locations
Now that you’ve tested it, you might want to serve your files from a specified site without going there. Let’s say you’re in your home directory and want to serve your files in the /code_files/scripts/ directory without having to cd in. Here’s how we’ll accomplish it.
pushd /code_files/scripts/; python –m SimpleHTTPServer 9779; popd;
Serving HTML Files
If your serving location contains an index.html file, the Python interpreter will automatically recognize it and serve the html file instead of your files. Let’s take a closer look. In our case, the file index.html is located in /code_files/scripts/ and has a simple html script.
<html> <header><title>CodeUnderscored</title></header> <body text="blue"><H1> Testing SimpleHTTPServer .html files. </H1> <p><a href="https://www.codeunderscored.com"> Serving HTML FIles</a></p> </body> </html>
Save it, then run SimpleHTTPServer on /code_files/scripts/ and navigate to the location using a web browser.
# pushd /code_files/scripts/; python –m SimpleHTTPServer 9779; popd;
Local execution of server-side languages
A Python web framework is required to run Python server-side code. Django, Flask, and Pyramid are some of the most popular Python web frameworks.
Launch PHP’s built-in development server to run PHP server-side code as follows:
- $ cd path/to/your/php/code
- $ php -S localhost:8000
It is very straightforward and practical. You can offer your files or your HTML code in a flash. The best part is the lack of worry about installing anything. You don’t have to copy a file to a shared location or make your directories shareable if you want to share a file with someone.
It’s as simple as running SimpleHTTPServer on it. When using this Python module, a few things to bear in mind. It runs on the terminal and prints out what happens inside when it serves files. When you access it using a browser or download a file, it displays your IP address and the file you downloaded. Isn’t it convenient?