Python 脚本在 headers 错误之前结束脚本输出

End of script output before headers error with Python Script

我已经阅读了多篇关于此的 SO 帖子,但似乎无法让它发挥作用。这是我第一次在 Apache 上使用 Python,所以我很感激能得到的帮助!

所以最终,我试图在我的 htdocs 中 运行 一个 Python 脚本,但我似乎不能只得到简单的 python 脚本 运行宁 XAMPP。我一直收到 500 错误:

myurl.py

#!/usr/bin/env python3

print("Content-Type: text/html")
print()
print ("""
    <TITLE>CGI script ! Python</TITLE>
    <H1>This is my first CGI script</H1>
    Hello, world!
"""
)

根据讨论,这里有多个问题,通过检查 apache 编写的 error.log 然后进行适当的更改解决了这些问题。

第一个错误是:

[Tue Nov 20 17:49:06.593901 2018] [cgi:error] [pid 47854] [client ::1:50462] AH01215: (13)Permission denied: exec of '/Applications/XAMPP/xamppfiles/htdocs/myurl.py' failed: /Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:49:06.595547 2018] [cgi:error] [pid 47854] [client ::1:50462] End of script output before headers: myurl.py

这里的相关部分是:

(13)Permission denied: exec of '/Applications/XAMPP/xamppfiles/htdocs/myurl.py' failed

需要在正在执行的 .py 文件上设置权限,以允许用户 运行 apache 进程执行脚本。这是使用 chmod.

完成的

然后,出现另一个错误:

[Tue Nov 20 17:59:04.720816 2018] [cgi:error] [pid 48715] [client ::1:50555] AH01215: python3: No such file or directory: /Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:59:04.720884 2018] [cgi:error] [pid 48715] [client ::1:50555] End of script output before headers: myurl.py

相关部分是:

python3: No such file or directory

这表明系统找不到要执行的 python3 二进制文件。 python3 解释器的正确路径必须使用 which python3 来确定。然后将其编辑到脚本的 shebang 行中。