Run Python HTTP Server Locally On 127.0.0.1: Step-by-Step

8 min read 11-15- 2024
Run Python HTTP Server Locally On 127.0.0.1: Step-by-Step

Table of Contents :

To run a Python HTTP server locally on your machine using the loopback address 127.0.0.1, you don't need much setup or any external software. This guide will take you through the steps of creating a simple HTTP server using Python, and it's perfect for those who want to serve files from their local directory or perform quick tests during development.

What You Need to Know About Python HTTP Server

Before we dive into the step-by-step process, let's clarify a few key points about the Python HTTP server:

  1. Built-in Module: Python comes with a built-in module called http.server (or SimpleHTTPServer in Python 2) that allows you to easily serve files over HTTP.
  2. Default Port: By default, the server runs on port 8000, but you can choose any available port for your server.
  3. Use Cases: This server is great for testing and development purposes. It is not recommended for production use due to its simplicity and lack of security features.

Now that we’ve set the stage, let’s jump into the steps!

Step 1: Install Python

First, ensure you have Python installed on your machine. Most systems come with Python pre-installed, but it's always good to check.

How to Check Python Installation

Open your command line interface (CLI) and type:

python --version

or for Python 3:

python3 --version

You should see an output like this:

Python 3.x.x

If you see a version number, you are good to go! If not, you can download and install Python from the .

Step 2: Navigate to Your Desired Directory

The HTTP server will serve files from the directory in which you run the server command. So, navigate to the directory you want to use. You can use the cd command (change directory) to navigate.

Example Command

cd path/to/your/directory

Replace path/to/your/directory with the actual path where your files are located.

Step 3: Start the Python HTTP Server

For Python 3

To start the HTTP server in Python 3, run the following command:

python3 -m http.server 8000

For Python 2

If you are using Python 2, use the command:

python -m SimpleHTTPServer 8000

Note

You can change 8000 to any other port number if you have a preference or if port 8000 is already in use.

Step 4: Accessing the Server

Once the server is running, you will see output similar to this:

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

You can now access your server by opening a web browser and navigating to:

http://127.0.0.1:8000

or simply:

http://localhost:8000

Important Note

“Make sure your firewall settings allow connections on the specified port to ensure your HTTP server can be accessed locally.”

Step 5: Stopping the Server

To stop the server, return to your terminal where the server is running and press Ctrl + C. This will terminate the HTTP server process.

Customizing Your HTTP Server

Specifying a Different Directory

If you want to serve files from a different directory without changing the terminal directory, you can specify the path directly in the command:

python3 -m http.server 8000 --directory /path/to/directory

Customizing the Port

As mentioned earlier, you can change the port to any number. Here is how you can specify a different port:

python3 -m http.server 8080

Table of Python HTTP Server Commands

Command Description
python3 -m http.server 8000 Start a simple HTTP server on port 8000.
python -m SimpleHTTPServer 8000 Start a simple HTTP server for Python 2 on port 8000.
python3 -m http.server 8080 Start a server on a different port, 8080.
python3 -m http.server --directory /path/to/directory Serve files from a specific directory.

Security Considerations

While running a local server for testing and development is convenient, always keep in mind some basic security practices:

  • Local Use Only: The server you set up using this method should not be exposed to the internet. It's meant for local development only.
  • Sensitive Data: Do not serve sensitive data over this server, as it lacks proper authentication and encryption.
  • File Access: Ensure that the directory you are serving does not have sensitive or unnecessary files accessible to others on your network.

Conclusion

Running a Python HTTP server locally on 127.0.0.1 is a straightforward process that can significantly streamline your development and testing workflow. With just a few commands, you can serve files and access them via your web browser without the need for complex setups. Whether you are a seasoned developer or a beginner looking to experiment with web technologies, understanding how to set up this server can be an invaluable skill.

Happy coding! 😊