什么是Python 3相当于 python -m SimpleHTTPServer ?
python -m SimpleHTTPServer
在我的一个项目中,我针对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 ...
作为别名:
$ 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 ...
请注意,我通过conda environments控制我的Python版本,因为我可以使用 python 而不是 python3 来使用Python 3 .
python
python3
相当于:
python3 -m http.server
命令 python -m SimpleHTTPServer 适用于Linux . 对Windows使用Command python -m http.server 7777
python -m http.server 7777
除了Petr的答案,如果你想绑定到特定的接口而不是所有的接口,你可以使用-b / - bind标志 .
python -m http.server 8000 --bind 127.0.0.1
上面的片段应该可以解决问题 . 8000是端口号 . 80用作HTTP通信的标准端口 .
来自the docs:
SimpleHTTPServer模块已合并到Python 3.0中的http.server中 . 将源转换为3.0时,2to3工具将自动调整导入 .
所以,你的命令是 python3 -m http.server .
使用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
6 回答
在我的一个项目中,我针对Python 2和3运行测试 . 为此,我编写了一个小脚本,它独立启动本地服务器:
作为别名:
请注意,我通过conda environments控制我的Python版本,因为我可以使用
python
而不是python3
来使用Python 3 .相当于:
命令
python -m SimpleHTTPServer
适用于Linux . 对Windows使用Commandpython -m http.server 7777
除了Petr的答案,如果你想绑定到特定的接口而不是所有的接口,你可以使用-b / - bind标志 .
上面的片段应该可以解决问题 . 8000是端口号 . 80用作HTTP通信的标准端口 .
来自the docs:
所以,你的命令是
python3 -m http.server
.使用2to3实用程序 .