Python 3与Python-m SimpleHTTPServer的等价物是什么?


当前回答

从文档中:

SimpleHTTPServer模块已合并到Python 3.0中的http.server中。2to3工具将在将源代码转换为3.0时自动调整导入。

因此,您的命令是python-m http.server,或者根据您的安装,它可以是:

python3 -m http.server

其他回答

从文档中:

SimpleHTTPServer模块已合并到Python 3.0中的http.server中。2to3工具将在将源代码转换为3.0时自动调整导入。

因此,您的命令是python-m http.server,或者根据您的安装,它可以是:

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

与许多*nix-utils一样,如果传递的参数为-,2to3将接受stdin。因此,您可以在不创建任何文件的情况下进行测试,如下所示:

$ 2to3 - <<< "import SimpleHTTPServer"

只是想补充一下对我有用的东西:python3-m http.server8000(您可以在此处使用任何端口号,但当前正在使用的端口号除外)

除了Petr的答案之外,如果您想绑定到特定接口而不是所有接口,可以使用-b或--bind标志。

python -m http.server 8000 --bind 127.0.0.1

上面的代码片段应该可以做到这一点。8000是端口号。80用作HTTP通信的标准端口。

等效值为:

python3 -m http.server