The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Switch On Looping

--

Chapters
00:00 What Is The Python 3 Equivalent Of &Quot;Python -M Simplehttpserver&Quot;
00:13 Answer 1 Score 371
00:21 Accepted Answer Score 2294
00:47 Answer 3 Score 163
01:17 Answer 4 Score 123
01:40 Thank you

--

Full question
https://stackoverflow.com/questions/7943...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x #httpserver #simplehttpserver

#avk47



ACCEPTED ANSWER

Score 2294


From the docs:

The SimpleHTTPServer module has been merged into http.server in Python 3.0. The 2to3 tool will automatically adapt imports when converting your sources to 3.0.

So, your command is python -m http.server, or depending on your installation, it can be:

python3 -m http.server



ANSWER 2

Score 371


The equivalent is:

python3 -m http.server



ANSWER 3

Score 163


Using 2to3 utility.

$ cat try.py
import SimpleHTTPServer

$ 2to3 try.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored try.py
--- try.py  (original)
+++ try.py  (refactored)
@@ -1 +1 @@
-import SimpleHTTPServer
+import http.server
RefactoringTool: Files that need to be modified:
RefactoringTool: try.py

Like many *nix utils, 2to3 accepts stdin if the argument passed is -. Therefore, you can test without creating any files like so:

$ 2to3 - <<< "import SimpleHTTPServer"



ANSWER 4

Score 123


In addition to Petr's answer, if you want to bind to a specific interface instead of all the interfaces you can use -b or --bind flag.

python -m http.server 8000 --bind 127.0.0.1

The above snippet should do the trick. 8000 is the port number. 80 is used as the standard port for HTTP communications.