本文翻译自:What is the Python 3 equivalent of “python -m SimpleHTTPServer”
什么是python -m SimpleHTTPServer
的Python 3等价物?
参考:https://stackoom.com/question/XKX1/什么是Python-相当于-python-m-SimpleHTTPServer
Using 2to3 utility. 使用2to3实用程序。
$ 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
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. 除了Petr的答案,如果你想绑定到特定的接口而不是所有接口,你可以使用-b / - bind标志。
python -m http.server 8000 --bind 127.0.0.1
The above snippet should do the trick. 上面的片段应该可以解决问题。 8000 is the port number. 8000是端口号。 80 is used as the standard port for HTTP communications. 80用作HTTP通信的标准端口。
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 2和3运行测试。为此,我编写了一个小脚本,它独立启动本地服务器:
$ 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. 请注意,我通过conda环境控制我的Python版本,因为我可以使用python
而不是python3
来使用Python 3。
相当于:
python3 -m http.server
The
SimpleHTTPServer
module has been merged intohttp.server
in Python 3.0.SimpleHTTPServer
模块已合并到Python 3.0中的http.server
中。 The 2to3 tool will automatically adapt imports when converting your sources to 3.0. 将源转换为3.0时,2to3工具将自动调整导入。
So, your command is python -m http.server
, or depending on your installation, it can be: 所以,你的命令是python -m http.server
,或者根据你的安装,它可以是:
python3 -m http.server