By ryanbraganza

2011-10-30 07:22:49 8 Comments

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


@shantanoo 2013-06-27 19:16:32

Using 2to3 utility.

$ cat
import SimpleHTTPServer

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

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"

@Eswar Yaganti 2017-05-30 15:47:12

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

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

@Haris Np 2019-05-15 12:22:23

python -m http.server 8081 --bind If your 8000 is being used by another program.

@Haris Np 2019-05-16 08:06:53

If you are not in a virtual environment where you are running Python3 , please use python3 -m http.server 8081 --bind, otherwise you will get an error that /usr/bin/python: No module named http

@Petr Viktorin 2011-10-30 07:28:58

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

@bseibold 2013-02-21 15:53:16

In Python 3.3, the replacement for python -m CGIHTTPServer is python3 -m http.server --cgi.

@CMCDragonkai 2014-06-04 08:24:36

Can you bind a different IP/Port with that command? How would I do that?

@Petr Viktorin 2014-06-04 18:51:36

Sure, just tack it on the end of the command line. Read python3 -m http.server --help for all the args & options.

@nu everest 2015-07-09 20:59:20

python -m http.server worked for me. I had to remove the 3

@Mast 2015-07-14 08:02:26

@nueverest It depends on how your Python installation is 'named'. Usually Python2 is available as python and Python3 as python3 but some prefer to install Python3 simply as python.

@Petr Viktorin 2015-07-14 20:10:58

AFAIK, on Windows, it'll install as just python by default. But, the question is for python3 :)

@K Zombro 2016-07-08 20:41:48

@CMCDragonkai python -m http.server --bind 8000 >>> Serving HTTP on port 8000 ...

@justinpitts 2018-03-04 14:11:42

python3 is now named 'python' for homebrew-managed installs on OSX.

@AdamKalisz 2018-08-22 08:35:47

@CMCDragonkai more examples at the end of this section:…

@fersarr 2018-10-11 13:14:38

is it possible to call this in a py2x and py3x compatible way? I tried using the six library but it doesnt seem to work with: 'python -m six.moves.SimpleHTTPServer'

@Paramvir Singh Karwal 2018-11-04 17:09:41

just installed python 3.7.1 on windows, neither python nor python3 worked, just py worked!

@SilentVoid 2019-02-11 18:53:14

@ParamvirSinghKarwal py is a wrapper on windows because Linux has a feature where if a script is marked executable and starts with a "shebang" (as in #! /bin/python3) executing it as a command (as in ./ will run it with that program so you can chose which version to run your script under, unless someone manually does python2 Windows uses just the files extension to decide which program to use so py is set to open .py files and looks for a shebang and then executes it under the correct python version.

@SilentVoid 2019-02-11 18:53:25

And on Arch I have python (Python 3), python2, python2.7, python3, and python3.7. If a had 2.6 for example, I'd also have a python2.6.

@dotbit 2019-07-17 16:32:41

another big fan of the " . " where people don't want one for copy & paste reasons. Must have been put there on purpose. <--- HERE a dot / full stop does belong, since there is no bash code involved, you see.

@Petr Viktorin 2019-07-19 07:48:09

@dotbit: That was an unfortunate result of an edit which turned an inline code example (like the python -m http.server) into a paragraph that had only code in it – but left the trailing comma. I've now turned it into a real code block. Please feel free to edit (or suggest edits) yourself when you see a mistake. Or just alert others if you see something that can be improved. There's no need for negativity :)

@Kuzeko 2020-03-13 08:40:17

@PetrViktorin what is the default --bind if I don't specify it? I looked into the docs but I cannot find it.

@Petr Viktorin 2020-03-16 10:05:32

@Kuzeko, the default is to listen on all interfaces ('').

@Darius 2017-10-05 22:32:20

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 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 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.

@Greg Hewgill 2011-10-30 07:27:48

The equivalent is:

python3 -m http.server

@AdamKalisz 2018-08-22 08:36:43

And python3 -m http.server 8080 if You need to bind to a port. Read more at the end of the section:…

@stephenwade 2019-03-20 21:24:54

By default, it will bind to port 8000. See python3 -m http.server --help for details.

Related Questions

Sponsored Content

22 Answered Questions

[SOLVED] What are metaclasses in Python?

62 Answered Questions

[SOLVED] How to call an external command?

33 Answered Questions

[SOLVED] What does if __name__ == "__main__": do?

26 Answered Questions

[SOLVED] How can I safely create a nested directory?

13 Answered Questions

[SOLVED] Iterating over dictionaries using 'for' loops

44 Answered Questions

42 Answered Questions

[SOLVED] What does the "yield" keyword do?

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

12 Answered Questions

[SOLVED] What is for?

Sponsored Content