如何使用`python -m SimpleHTTPServer 8000`或`python -m CGIHTTPServer 8000`托管python cgi脚本?

Ben*_*ey4 22 python hosting cgi simplehttpserver

当我运行python -m SimpleHTTPServer 8000python -m CGIHTTPServer 8000在我的shell中时,我将当前目录的内容托管到互联网上.

我想在浏览时使用命令行中的上述命令使以下cgi_script.py正常工作 192.xxx.x.xx:8000/cgi_script.py

#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
Run Code Online (Sandbox Code Playgroud)

但这个脚本按字面显示,而不仅仅是"Hello World!" 部分.顺便说一句,我将文件权限更改为755 cgi_script.py以及我托管它的文件夹.

rod*_*igo 33

试试吧python -m CGIHTTPServer 8000.

请注意,您必须将脚本移动到cgi-binhtbin目录才能运行.

  • @ Bentley4 - 不是很奇怪,这是一个经典的旧限制,以避免运行随机程序,就好像它们是CGI,当它们不打算以这种方式使用时. (2认同)

spa*_*edc 6

因此我不允许我发表评论所以我将这个作为一个单独的答案加入,除了罗德里戈之外.

您可以使用cgi_directories默认的其他参数['/cgi-bin', '/htbin'].更多信息在这里