What is the Python 3 equivalent of "python -m SimpleHTTPServer"
Fire up an HTTP server in Python 3 using the http.server
module as follows:
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
):
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:
Playing with port numbers
Stuck with extra traffic on port 8000 or just bored? Switch to any free port on your system:
Hello from the IPv6 world
Got Python 3.8 or higher? Wave hello to IPv6. Use an IPv6 address in the bind argument:
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:
This will display what changes are needed for your Python 2 SimpleHTTPServer
code to work like a charm in Python 3.
Was this article helpful?