1 前言
在学习Python CGI编程时,运行起来总是有各种各样的问题,故将问题进行总结,以便新接触Python的童鞋能少走弯路
以下均为本人遇到对应报错的解决方案,可能存在其他问题但报错相同的情况,若以下解决方案无效,请换方向排查
2 测试文件
#!/usr/bin/python3
print ("Content-type:text/html")
print () # 空行,告诉服务器结束头部
print ('<html>')
print ('<head>')
print ('<meta charset="utf-8">')
print ('<title>Hello Word - 我的第一个 CGI 程序!</title>')
print ('</head>')
print ('<body>')
print ('<h2>Hello Word! 我是来自菜鸟教程的第一CGI程序</h2>')
print ('</body>')
print ('</html>')
3 问题总结
Linux 系统下报错AH01215: (2)No such file or directory: exec of ‘/var/www/cgi-bin/hello.py’ failed
Linux 下与 Windows 下编码方式不同,导致从 Windows 复制的文件在 Linux 下找不到#!/usr/bin/python3
行,使 cgi 脚本没有找到环境,尝试删除对应文件并在 Linux 下重新创建Windows 系统下报错(OS 2)系统找不到指定的文件。 : [client ::1:55264] AH01223: couldn’t spawn child process
python 路径不正确,修改第一行为#!C:/python3
,注意,C:/python3
为真实python.exe
对应目录,且必须放在文件最前面Windows 系统下报错You don’t have permission to access this resource.AH01630: client denied by server configuration:
httpd.conf 文件ScriptAlias
和对应Directory
路径配置错误,ScriptAlias /cgi-bin/ "C:/var/www/cgi-bin/"
为真实路径,最后一个/
必须存在且都不能为\
Windows 系统下中文出现乱码
在第二行增加# coding: utf8
,并删除print ('<meta charset="utf-8">')