【问题标题】:Running python script on local server only shows source code在本地服务器上运行 python 脚本只显示源代码
【发布时间】:2016-05-03 17:40:14
【问题描述】:

我有一个关于 Python 运行问题的问题。 我使用python -m http.server 8000 命令在Windows 命令提示符下设置了一个本地服务器,但是当我在http://localhost:8000/cgi-bin/hello.py 上运行我的脚本时,它只显示源代码而不是实际运行脚本。

我使用我在this 链接上找到的代码。

#!/usr/bin/python

print('Content-Type: text/html')
print()
print('<html>')
print('<head><title>Hello from Python</title></head>')
print('<body>')
print('<h2>Hello from Python</h2>')
print('</body></html>')

我见过几个人遇到同样的问题,但我真的找不到解决方案。 我使用 Python 3.5

【问题讨论】:

  • 你是用python -m CGIHTTPServer开始的吗?喜欢在您链接的页面上?
  • 按照该页面上的所有说明进行操作。不只是其中一些。 -1
  • 当我尝试使用 python -m CGIHTTPServer 时,它不起作用,我查看了 python 文档,发现他们将所有服务器命令合并到 python -m http.server 命令中。不是一样的吗?
  • 没有。 http.server 为目录中的文件提供服务,如果您有静态 html 页面,它会很有用。要执行 python 应用程序(或 PHP 或任何其他生成 html 文件的动态语言),您需要 CGI 或其他一些网关接口。现在正如@Daniel 建议的那样,您可以使用 --cgi 选项。

标签: python


【解决方案1】:

您应该关注the documentation。 Python 3 的命令是:

python -m http.server --cgi 8000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2019-10-21
    • 2018-12-17
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 2014-12-03
    相关资源
    最近更新 更多