首页 > 解决方案 > End of script output before headers error with Python Script

问题描述

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!

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:

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!
"""
)

标签: pythonxampp

解决方案


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

第一个错误是:

[2018 年 11 月 20 日星期二 17:49:06.593901] [cgi:error] [pid 47854] [client ::1:50462] AH01215: (13)Permission denied: exec of '/Applications/XAMPP/xamppfiles/htdocs/myurl. py'失败:/Applications/XAMPP/xamppfiles/htdocs/myurl.py [Tue Nov 20 17:49:06.595547 2018] [cgi:error] [pid 47854] [client ::1:50462] 头文件前的脚本输出结束: myurl.py

这里的相关部分是:

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

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

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

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

相关部分是:

python3: No such file or directory

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


推荐阅读