Python CGI脚本在XAMPP中不执行
我刚开始学习Python,安装了Python 3.4和XAMPP。我修改了http.config文件,并在处理程序块中添加了.py扩展名。然后我把我的Python脚本放到了xamp/bin-cgi文件夹里,并把脚本的第一行设置为“#!C:/Python34/python.exe”。但是当我通过localhost/cgi-bin/test.py打开这个文件时,只显示了一个空白的屏幕,下面是文件的内容。
#!C:/Python34/python.exe
print "Content-type: text/html"
print
print "<html>"
print "<head>"
print "<title>welcome cgi</title>"
print "</head>"
print "<body>"
print "<h1>first python page</h1>"
print "<p>heihei</p>"
print "</body>"
print "</html>"
2 个回答
0
你正在使用 Python 2.7 来写打印语句,这就是第一个错误。你其实是在调用 Python 3.4 的解释器。
另外,你需要把第一行改成
#!/Python34/python
# -*- coding: UTF-8 -*-
# enable debugging
然后,把你的打印语句改成带括号的形式
print("Content-type: text/html")
print()
print("""<html>
<head>
<title>welcome cgi</title>
</head>
<body>
<h1>first python page</h1>
<p>heihei</p>
</body>
</html>""")
注意:
如果你注意到,我稍微改了一下你的代码,去掉了很多打印语句。相反,我用了一个叫做 多行字符串
的东西(与其写 "blah"
,我会写 """blah"""
)。比如说,如果我这样写
print("asdasdasd
asdasdasdasdasd")
这样是行不通的
但是,如果我改成
print("""asdasdasdasdasd
asdasdasdasdasd""")
这样就是一个完全可以接受的命令。每一行新开头都会被当作 "\n"
处理,所以实际上,我们打印出来的字符串是 "asdasdasdasdasd\nasdasdasdasdasd"
,其中 \n
表示换行。
0
你应该把第一行改成这样:
#!"C:\Python34\python.exe"