Python, with its vast array of libraries, allows us to create a simple HTTP server with just a few lines of code. This can be incredibly useful for serving stat...

Python, with its vast array of libraries, allows us to create a simple HTTP server with just a few lines of code. This can be incredibly useful for serving static files, testing web applications, or even hosting a simple website. In this article, we'll guide you through the process of starting a Python HTTP server using the built-in `http.server` module and `http.server` command.

The `http.server` module in Python provides a simple HTTP server that can serve files from a specified directory. It's a part of Python's standard library, so you don't need to install any additional packages to use it. The module defines a single class, `HTTPServer`, which you can use to create a server, and a function, `test`, which you can use to start a server with a single line of code.

To start a Python HTTP server, you can use the `http.server` command in your terminal or command prompt. Here's the basic syntax:

python -m http.server [options]
Here's a breakdown of the command:
python: Invokes the Python interpreter.-m: Tells Python to run the specified module as a script.http.server: The name of the module we want to run.[options]: Optional arguments that you can pass to the server.
If you run the command without any options, the server will start on port 8000 and serve files from the current directory. Here's how you can do it:
python -m http.server
After running this command, you should see output similar to this:
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
This means your server is now running and listening for requests on port 8000.

You can also start the server with specific options. Here are a few common ones:




















| Option | Description |
|---|---|
-d |
Runs the server in the background and detaches it from the current shell. |
-b host:port |
Binds the server to a specific host and port. For example, -b 127.0.0.1:8080. |
-h |
Shows a help message with all available options. |
Here's an example of starting the server in the background and binding it to a specific host and port:
python -m http.server -d -b 127.0.0.1:8080
Once your server is running, you can access it using a web browser or a tool like `curl`. If you started the server on your local machine, you can access it by navigating to http://localhost:8000 in your browser. If you bound the server to a specific IP address and port, you can access it by navigating to http://.
For example, if you started the server with python -m http.server -b 127.0.0.1:8080, you can access it by navigating to http://127.0.0.1:8080 in your browser.
To stop the server, you can use `Ctrl+C` in your terminal or command prompt. If you started the server in the background using the `-d` option, you'll need to find the process ID (PID) and use the `kill` command to stop it.
Here's how you can find the PID and stop the server:
http with the name of your server):ps aux | grep 'http.server'
kill
That's it! You now know how to start, access, and stop a simple HTTP server using Python's `http.server` module and command.