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

0 votes
292 views
asked Sep 28, 2021 by Hitesh Garg (799 points)  

In Python2 you can create a simple HTTP server using command python -m SimpleHTTPServer but how to do the same thing in Python 3?

commented Sep 28, 2021 by Hitesh Garg (799 points)  
The equivalent command in Python 3 is
python3 -m http.server

1 Answer

0 votes
answered Sep 28, 2021 by Hitesh Garg (799 points)  
 
Best answer

The equivalent in Python3 is

python3 -m http.server

But as a warning from https://docs.python.org/3/library/http.server.html#module-http.server

Warning: http.server is not recommended for production. It only
implements basic security checks.


Details

http.server can also be invoked directly using the -m switch of the interpreter.

python -m http.server

The above command will run a server by default on port number 8000.

You can also give the port number explicitly while running the server

python -m http.server 9000

The above command will run an HTTP server on port 9000 instead of 8000.

The option -b/--bind specifies a specific address to which it should bind.
It supports both IPv4 and IPv6 addresses.
For example, the following command binds the server to localhost only:

python -m http.server 8000 --bind 127.0.0.1

or

python -m http.server 8000 -b 127.0.0.1

Directory Binding

By default, the server uses the current directory. The option -d/--directory specifies a directory to which it should serve the files.
Following command shows how to use a specific directory

python -m http.server --directory /tmp/
...