What is the Python 3 equivalent of “python -m Simp

2019-01-07 01:10发布

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

6条回答
趁早两清
2楼-- · 2019-01-07 01:36

The equivalent is:

python3 -m http.server
查看更多
一夜七次
3楼-- · 2019-01-07 01:40

The command python -m SimpleHTTPServer is for Linux. Use Command python -m http.server 7777 for Windows

查看更多
Lonely孤独者°
4楼-- · 2019-01-07 01:41

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
查看更多
做自己的国王
5楼-- · 2019-01-07 01:54

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.

查看更多
Explosion°爆炸
6楼-- · 2019-01-07 01:59

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 python3 -m http.server.

查看更多
爷、活的狠高调
7楼-- · 2019-01-07 02:01

In addition to Petr's answer, if you want to bind to a specific interface instead of all the interfaces you can use -b/--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.

查看更多
登录 后发表回答