python -m SimpleHTTPServer"在Python 3中等价的命令是什么?
在Python 3中,python -m SimpleHTTPServer
的对应命令是什么呢?
7 个回答
163
使用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
就像很多*nix工具一样,2to3
可以接受stdin
,如果你传入的参数是-
。所以,你可以这样测试,而不需要创建任何文件:
$ 2to3 - <<< "import SimpleHTTPServer"
370
等价的写法是:
python3 -m http.server
2290
来自官方文档:
SimpleHTTPServer
模块在Python 3.0中已经合并到了http.server
里。使用2to3工具时,它会在你把代码转换成3.0版本时自动调整导入的内容。
所以,你可以用的命令是python -m http.server
,或者根据你的安装情况,可能是:
python3 -m http.server