How to Set up a Local HTTP Server in Python

Posted: | Tags: Python

You can quickly set up a local HTTP server using Python’s built-in http.server module, which is especially useful for development and testing.

Start the server with the following command. Press Ctrl + C to stop it.

$ python -m http.server

Note that http.server is designed for development use only and is not recommended for production.

Warning: http.server is not recommended for production. It only implements basic security checks. http.server — HTTP servers — Python 3.13.3 documentation

Starting the server with python -m http.server

The -m option with the python (or python3) command allows you to run a module as a script.

Running the http.server module this way starts a simple HTTP server that serves files from the current directory.

$ python -m http.server
Serving HTTP on :: port 8000 (http://[::]:8000/) ..

By default, the server listens on port 8000. You can access it in your browser via:

If there's an index.html file in the current directory, it will be served automatically.

Stopping the server with Ctrl + C

To stop the server, press Ctrl + C in the terminal or command prompt where the server is running.

Local network access

To access the server from another device on the same local network—such as a smartphone for testing—use the private IP address of the machine running the server.

To find your private IP address on macOS or Windows, refer to the following articles:

For example, if the server machine’s IP address is 192.168.11.1, you can access it from another device via http://192.168.11.1:8000.

To restrict access to only the local machine, you can use the -b (or --bind) option, as explained later.

Changing the port number

You can specify a custom port number by adding it as an argument. For example, to use port 9000:

$ python -m http.server 9000

Changing the root directory

By default, the server uses the current directory as the root. You can specify a different directory using the -d or --directory option:

$ python -m http.server --directory path/to/dir/

Restricting access to the local machine

By default, the server is accessible from other devices on the same local network.

To restrict access so that only the machine running the server can connect, use the -b or --bind option with 127.0.0.1 or localhost:

$ python -m http.server -b 127.0.0.1

Related Categories

Related Articles