Explain Codes LogoExplain Codes Logo

What is the Python 3 equivalent of "python -m SimpleHTTPServer"

python
http-server
local-development
ipv6
Alex KataevbyAlex Kataev·Aug 3, 2024
TLDR

Fire up an HTTP server in Python 3 using the http.server module as follows:

python3 -m http.server 8000

This serves the current directory over HTTP on the chosen port, here being 8000. Omitting the port defaults to 8000.

Binding to a local address in Python 3

You might not want the whole world peeking into your files on your dev server. To serve contents strictly to local requests, bind to a specific IP address (127.0.0.1):

python3 -m http.server --bind 127.0.0.1 8000 # nobody can peek into your secret cookie recipe now!

Serving a specific directory

You've got the basics down, but what if you just want to serve files from a specific place? Use the --directory switch:

python3 -m http.server --directory /path/to/directory 8000 # serving my awesome cat pictures!

Playing with port numbers

Stuck with extra traffic on port 8000 or just bored? Switch to any free port on your system:

python3 -m http.server 9000 # because nine is a cool number!

Hello from the IPv6 world

Got Python 3.8 or higher? Wave hello to IPv6. Use an IPv6 address in the bind argument:

python3 -m http.server --bind ::1 # Hello, next-gen Internet!

Friendly warning

Though http.server is a fantastic tool for development, never deploy it in a production environment. Its lack of extensive security features makes it a bad guy magnet!

Running that old Python 2 code

Need to run Python 2 code on Python 3? Use the 2to3 tool and breathe easy:

2to3 - <<< "import SimpleHTTPServer" # Ancient artifact processing...

This will display what changes are needed for your Python 2 SimpleHTTPServer code to work like a charm in Python 3.