What is the Python 3 equivalent of python -m SimpleHTTPServer?
7 Answers
From the docs:
The
SimpleHTTPServermodule has been merged intohttp.serverin 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
-
99In Python 3.3, the replacement for
python -m CGIHTTPServerispython3 -m http.server --cgi.– bseiboldCommented Feb 21, 2013 at 15:53 -
27Sure, just tack it on the end of the command line. Read
python3 -m http.server --helpfor all the args & options.– ᅠᅠᅠCommented Jun 4, 2014 at 18:51 -
34
python -m http.serverworked for me. I had to remove the3Commented Jul 9, 2015 at 20:59 -
16@nueverest It depends on how your Python installation is 'named'. Usually Python2 is available as
pythonand Python3 aspython3but some prefer to install Python3 simply aspython.– MastCommented Jul 14, 2015 at 8:02 -
6AFAIK, on Windows, it'll install as just
pythonby default. But, the question is forpython3:)– ᅠᅠᅠCommented Jul 14, 2015 at 20:10
The equivalent is:
python3 -m http.server
-
83And
python3 -m http.server 8080if You need to bind to a port. Read more at the end of the section: docs.python.org/3/library/… Commented Aug 22, 2018 at 8:36 -
5By default, it will bind to port 8000. See
python3 -m http.server --helpfor details. Commented Mar 20, 2019 at 21:24
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"
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.
-
python -m http.server 8081 --bind 127.0.0.1 If your 8000 is being used by another program. Commented May 15, 2019 at 12:22
-
If you are not in a virtual environment where you are running Python3 , please use python3 -m http.server 8081 --bind 127.0.0.1, otherwise you will get an error that /usr/bin/python: No module named http Commented May 16, 2019 at 8:06
As everyone has mentioned http.server module is equivalent to python -m SimpleHTTPServer.
But as a warning from https://docs.python.org/3/library/http.server.html#module-http.server
Warning:
http.serveris not recommended for production. It only implements basic security checks.
Usage
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.
By default, server binds itself to all interfaces. The option -b/--bind specifies a specific address to which it should bind. Both IPv4 and IPv6 addresses are supported. For example, the following command causes the server to bind to localhost only:
python -m http.server 8000 --bind 127.0.0.1
or
python -m http.server 8000 -b 127.0.0.1
Python 3.8 version also supports IPv6 in the bind argument.
Directory Binding
By default, server uses the current directory. The option -d/--directory specifies a directory to which it should serve the files. For example, the following command uses a specific directory:
python -m http.server --directory /tmp/
Directory binding is introduced in python 3.7
-
Everyone mentions "Warning: http.server is not recommended for production. It only implements basic security checks." but do you have any suggestions for easy to use file servers as alternatives. I have a docker app and I would like to run something like this server in a separate container behind nginx. Any suggestions? Commented Nov 10, 2021 at 1:12
In one of my projects I run tests against Python 2 and 3. For that I wrote a small script which starts a local server independently:
$ python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")')
Serving HTTP on 0.0.0.0 port 8000 ...
As an alias:
$ alias serve="python -m $(python -c 'import sys; print("http.server" if sys.version_info[:2] > (2,7) else "SimpleHTTPServer")')"
$ serve
Serving HTTP on 0.0.0.0 port 8000 ...
Please note that I control my Python version via conda environments, because of that I can use python instead of python3 for using Python 3.
Just wanted to add what worked for me:
python3 -m http.server 8000 (you can use any port number here except the ones which are currently in use)