【问题标题】:What is the Python 3 equivalent of "python -m SimpleHTTPServer"“python -m SimpleHTTPServer”的 Python 3 等价物是什么
【发布时间】:2011-12-18 03:05:38
【问题描述】:

python -m SimpleHTTPServer 的 Python 3 等效项是什么?

【问题讨论】:

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

标签: python python-3.x httpserver simplehttpserver


【解决方案1】:

来自the docs

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

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

python3 -m http.server

【讨论】:

  • 在 Python 3.3 中,python -m CGIHTTPServer 的替换是 python3 -m http.server --cgi
  • 当然,把它加在命令行的末尾。阅读python3 -m http.server --help 了解所有参数和选项。
  • python -m http.server 为我工作。我不得不删除3
  • @nueverest 这取决于您的 Python 安装是如何“命名”的。通常 Python2 以python 提供,Python3 以python3 提供,但有些人更喜欢简单地以python 安装 Python3。
  • AFAIK,在 Windows 上,默认安装为 python。但是,问题是python3 :)
【解决方案2】:

等价于:

python3 -m http.server

【讨论】:

  • python3 -m http.server 8080 如果您需要绑定到端口。在本节末尾阅读更多内容:docs.python.org/3/library/…
  • 默认绑定到8000端口,详见python3 -m http.server --help
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    python -m http.server 8000 --bind 127.0.0.1
    

    上面的 sn-p 应该可以解决问题。 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:没有名为http的模块
    【解决方案5】:

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

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

    用法

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

    python -m http.server
    

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

    python -m http.server 9000
    

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

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

    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 后面的单独容器中运行类似这个服务器的东西。有什么建议吗?
    【解决方案6】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2011-09-03
      • 2011-03-07
      • 2013-10-18
      • 2019-04-02
      相关资源
      最近更新 更多