ChatGPT解决这个技术问题 Extra ChatGPT

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

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

python -m http.server 8000 ,它将在端口 8000 上启动服务器

P
Petr Viktorin

the docs

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

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

python3 -m http.server

在 Python 3.3 中,python -m CGIHTTPServer 的替换是 python3 -m http.server --cgi
当然,只需将其添加到命令行的末尾即可。阅读 python3 -m http.server --help 以了解所有 args &选项。
python -m http.server 为我工作。我必须删除 3
@nueverest 这取决于您的 Python 安装是如何“命名”的。通常 Python2 以 python 的形式提供,Python3 以 python3 的形式提供,但有些人更喜欢将 Python3 简单地以 python 的形式安装。
AFAIK,在 Windows 上,它默认安装为 python。但是,问题是 python3 :)
G
Greg Hewgill

等效的是:

python3 -m http.server

python3 -m http.server 8080 如果您需要绑定到端口。阅读本节末尾的更多信息:docs.python.org/3/library/…
默认情况下,它将绑定到端口 8000。有关详细信息,请参阅 python3 -m http.server --help
B
Bruno Bronosky

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

s
simhumileco

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

python -m http.server 8000 --bind 127.0.0.1

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


python -m http.server 8081 --bind 127.0.0.1 如果你的 8000 正在被另一个程序使用。
如果你不在运行 Python3 的虚拟环境中,请使用 python3 -m http.server 8081 --bind 127.0.0.1,否则会报错 /usr/bin/python: No module named http
A
Anand Tripathi

正如大家所提到的,http.server 模块等同于 python -m SimpleHTTPServer
但作为来自 https://docs.python.org/3/library/http.server.html#module-http.server 的警告

警告:不建议将 http.server 用于生产。它只实现基本的安全检查。

用法

http.server 也可以使用解释器的 -m 开关直接调用。

python -m http.server

默认情况下,上述命令将在端口号 8000 上运行服务器。您还可以在运行服务器时明确给出端口号

python -m http.server 9000

上面的命令将在端口 9000 而不是 8000 上运行 HTTP 服务器。

默认情况下,服务器将自己绑定到所有接口。选项 -b/--bind 指定它应该绑定的特定地址。支持 IPv4 和 IPv6 地址。例如,以下命令使服务器仅绑定到 localhost:

python -m http.server 8000 --bind 127.0.0.1

或者

python -m http.server 8000 -b 127.0.0.1

Python 3.8 版本还支持绑定参数中的 IPv6。

目录绑定

默认情况下,服务器使用当前目录。选项 -d/--directory 指定它应该向其提供文件的目录。例如,以下命令使用特定目录:

python -m http.server --directory /tmp/

在 python 3.7 中引入了目录绑定


大家都提到“警告:http.server 不推荐用于生产。它只实现基本的安全检查。”但是您对易于使用的文件服务器作为替代品有什么建议吗?我有一个 docker 应用程序,我想在 nginx 后面的单独容器中运行类似这个服务器的东西。有什么建议么?
D
Darius

在我的一个项目中,我针对 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。


b
benson23

只是想添加对我有用的内容:python3 -m http.server 8000(您可以在此处使用任何端口号,除了当前正在使用的端口号)