[英]python cgi script is not executing in XAMPP
我是python的初学者。 我已经安装了python34和xampp。 我已经更改了http.config文件,并在处理程序块中添加了.py扩展名。 然后我将python脚本放入xamp / bin-cgi中,并将python脚本的第一行设置为“#!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>"
您应该像这样重写第一行:
#!"C:\Python34\python.exe"
您将Python 2.7与print语句一起使用。 那是第一个错误。 您正在调用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>""")
如果您注意到了,我稍微修改了一下代码,并摆脱了一堆打印语句。 取而代之的是,我使用了所谓的multiline string
(而不是写"blah"
而是使用"""blah"""
)。 例如,如果我做了
print("asdasdasd
asdasdasdasdasd")
这行不通
但是,如果我将其更改为
print("""asdasdasdasdasd
asdasdasdasdasd""")
这将是一个完全可以接受的命令。 每个新行都将注册为"\\n"
因此,实际上,我们要打印的字符串是"asdasdasdasdasd\\nasdasdasdasdasd"
,其中\\n
表示新行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.