Python3的等价物是什么;python-msimplehttpserver“;
Python 3与Python-m SimpleHTTPServer的等价物是什么?等价物是:Python3的等价物是什么;python-msimplehttpserver“;,python,python-3.x,httpserver,simplehttpserver,Python,Python 3.x,Httpserver,Simplehttpserver,Python 3与Python-m SimpleHTTPServer的等价物是什么?等价物是: python3 -m http.server 发件人: 在Python 3.0中,SimpleHTTPServer模块已合并到http.server。将源代码转换为3.0时,2to3工具将自动调整导入 因此,您的命令是python-mhttp.server,或者根据您的安装情况,它可以是: python3 -m http.server 使用2to3实用程序 $ cat try.py import
python3 -m http.server
发件人:
在Python 3.0中,SimpleHTTPServer
模块已合并到http.server
。将源代码转换为3.0时,2to3工具将自动调整导入
因此,您的命令是
python-mhttp.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 util一样,2to3
接受stdin
,如果传递的参数是-
。因此,您可以在不创建任何文件的情况下进行测试,如下所示:
$ 2to3 - <<< "import SimpleHTTPServer"
$2to3-除了Petr的答案之外,如果您想绑定到一个特定的接口而不是所有的接口,您可以使用-b
或--bind
标志
python -m http.server 8000 --bind 127.0.0.1
上面的代码片段应该可以做到这一点。8000是端口号。80用作HTTP通信的标准端口。在我的一个项目中,我对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 ...
请注意,我通过控制我的Python版本,因为我可以使用Python
而不是python3
来使用Python 3。正如大家提到的那样,模块相当于Python-m SimpleHTTPServer
。
但是作为一个来自
警告:http.server
不建议用于生产。它只执行基本的安全检查
用法
还可以使用解释器的-m
开关直接调用http.server
python -m http.server
默认情况下,上述命令将在端口号8000
上运行服务器。您还可以在运行服务器时显式提供端口号
python -m http.server 9000
上面的命令将在端口9000而不是8000上运行HTTP服务器
默认情况下,服务器将自己绑定到所有接口。选择权
-b/--bind指定它应该绑定到的特定地址。IPv4和IPv6地址都受支持。例如,以下
命令使服务器仅绑定到本地主机:
或
Python3.8版本在bind参数中也支持IPv6
目录绑定
默认情况下,服务器使用当前目录。选项-d/--directory
指定了它应该为文件提供服务的目录。例如,以下命令使用特定目录:
python -m http.server --directory /tmp/
目录绑定是在python3.7中引入的,在python3.3中,python-mcgihttpserver
的替代品是python3-mhttp.server--cgi
。当然,只需在命令行的末尾添加它。阅读python3-mhttp.server--help
了解所有的参数和选项。python-mhttp.server
对我很有用。我不得不删除3
@numeverest,这取决于Python安装的“命名”方式。通常Python2可以作为python
使用,Python3可以作为Python3
使用,但有些人更喜欢将Python3简单地作为python
安装。另外,在Windows上,默认情况下,它将作为python
安装。但是,如果您需要绑定到端口,问题在于python3
:)和python3-mhttp.server8080
。阅读本节末尾的更多内容:默认情况下,它将绑定到端口8000。有关详细信息,请参见python3-mhttp.server--help
。python-mhttp.server 8081--bind 127.0.0.1,如果您的8000正在被其他程序使用。如果您不在运行python3的虚拟环境中,请使用python3-mhttp.server 8081--bind 127.0.0.1,否则您将得到一个错误/usr/bin/python:No module name httppython-m http.server 8000,它将在端口8000I上启动服务器。我非常喜欢您答案中的教学方面!
python -m http.server 8000 -b 127.0.0.1
python -m http.server --directory /tmp/