Python 脚本在标头错误之前的脚本输出结束 [英] End of script output before headers error with Python Script

查看:30
本文介绍了Python 脚本在标头错误之前的脚本输出结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've read multiple SO posts regarding this, but can't seem to get this to work. This is my first time working with Python on Apache so I would appreciate the help I can get!

所以最终,我试图在我的 htdocs 中运行一个 Python 脚本,但我似乎无法让简单的 Python 脚本在 XAMPP 上运行.我不断收到 500 错误:

So ultimately, I'm trying to run a Python script in my htdocs, but I can't seem to just get the simple python script running on XAMPP. I keep getting a 500 error:

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然后制作适当的改变.

As per the discussion, there were multiple problems here, which were solved by examining the error.log written by apache and then making appropriate changes.

第一个错误是:

[Tue Nov 20 17:49:06.593901 2018] [cgi:error] [pid 47854] [client::1:50462] AH01215: (13) 权限被拒绝:执行'/Applications/XAMPP/xamppfiles/htdocs/myurl.py' 失败:/Applications/XAMPP/xamppfiles/htdocs/myurl.py [11 月 20 日星期二17:49:06.595547 2018] [cgi:error] [pid 47854] [client ::1:50462] 结束标头前的脚本输出:myurl.py

[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 完成的.

Permissions needed to be set on the .py file being executed to allow the user running the apache process to execute the script. This was done using chmod.

然后,出现了另一个错误:

Then, another error was presented:

[Tue Nov 20 17:59:04.720816 2018] [cgi:error] [pid 48715] [client::1:50555] AH01215: python3: 没有这样的文件或目录:/Applications/XAMPP/xamppfiles/htdocs/myurl.py [11 月 20 日星期二17:59:04.720884 2018] [cgi:error] [pid 48715] [client ::1:50555] 结束标头前的脚本输出:myurl.py

[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 行中.

This shows that the system could not find a python3 binary to execute. The correct path to the python3 interpreter had to be determined using which python3. This was then edited into the shebang line of the script.

这篇关于Python 脚本在标头错误之前的脚本输出结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆