什么是与Python 3等价的“ python -m SimpleHTTPServer”

问题:什么是与Python 3等价的“ python -m SimpleHTTPServer”

Python 3等效于python -m SimpleHTTPServer什么?

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


回答 0

文档

SimpleHTTPServer模块已合并到http.serverPython 3.0中。将源转换为3.0时,2to3工具将自动适应导入。

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

python3 -m http.server

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 python -m http.server, or depending on your installation, it can be:

python3 -m http.server

回答 1

等效为:

python3 -m http.server

The equivalent is:

python3 -m http.server

回答 2

使用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

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

回答 3

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

python -m http.server 8000 --bind 127.0.0.1

上面的代码段应该可以解决问题。端口号8000。80用作HTTP通信的标准端口。

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


回答 4

在我的一个项目中,我针对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环境控制我的Python版本,因此可以python代替python3使用Python 3。

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.