如何执行Python CGI脚本?

5
我想在一个.shtml文件中执行Python CGI脚本,但我无法弄清楚如何做到这一点。我已经找到了几种方法,但似乎没有任何方法可以正常工作。很难找到真正显示如何执行脚本的信息,而不是如何编写脚本! ;/
我的Html: http://pastebin.com/4sNZTZNQ 我的脚本: http://pastebin.com/w5vGXCBp 我对CGI和任何Webstuff都很陌生,但我已经用Python编程半年了。
PS:对于代码的混乱格式化感到抱歉,我现在将其上传到pastebin :S
//编辑: 好的,现在提供更多信息,因为它仍然无法正常工作。
从heliohost.org上,我被引导到http://area52.heliohost.org/cgi-bin/snakecharmer.py,在那里您可以找到python解释器的路径。
这是我的文件夹结构: - public_html - .htaccess - main.py - index.shtml
.htaccess的内容:
Options +ExecCGI
AddHandler cgi-script .py

main.py的内容:

#! /usr/local/bin/python
print "Content-Type: text/html"
print

print "Hello World!"

index.shtml的内容如下: http://pastebin.com/Trg8sXBc

现在,当我点击链接时,会出现"500内部服务器错误",我不明白为什么。:(

服务器错误日志只显示了这些:

[Wed Jun 15 14:41:26 2011] [error] [client 84.151.252.129] File does not exist: /home/nux95/public_html/500.shtml, referer: http: niklasrosenstein.heliohost.org/

http://heliohost.org/ 感谢您的快速回复。 - Niklas R
PS:我在移动设备上,请原谅我的拼写问题。^^ - Niklas R
你在浏览器输出中看到SSI标签了吗? - Ignacio Vazquez-Abrams
就像我说的,查看错误日志。 - Ignacio Vazquez-Abrams
嗯,在原帖中看到编辑,请等待我完成它。 - Niklas R
显示剩余8条评论
2个回答

8

这是我一段时间前写的内容。

有一些技巧可使Python在CGI中工作。

  1. Always browse the pages through Apache. Note that viewing files in the filesystem through a browser works for most things on an html page but will not work for CGI. For scripts to work they must be opened through the htdocs file system. The address line of your browser should look like:

    \\127.0.0.1\index.html or
    \\localhost\index.html
    

    If you open a file up through the file system the CGI will not work. Such as if this is in the location bar of your browser:

    c:\Apache\htdocs\index.html (or some other example location)
    
  2. Convert end of lines of scripts to Unix format: Most editors have options to "show end of lines" and then a tool to convert from Unix to PC format. You must have the end of lines set to Unix format.

  3. State the path to the Python interpreter on the first line of the CGI script: You must have one of the following lines as the first line of your Python CGI script:

    #!C:\Python25\Python.exe
    #!/usr/bin/python
    

    The top line is used when you are debugging on a PC and the bottom is for a server such as 1and1. I leave the lines as shown and then edit them once they are up on the server by deleting the first line.

  4. Print a content type specifying HTML before printing any other output: This can be done simply by adding the following line somewhere very early in your script:

    print "Content-Type: text/html\n\n"
    

    Note that 2 end of lines are required.

  5. Setup Python scripts to give debugging information: Import the following to get detailed debugging information.

    import cgitb; cgitb.enable()
    

    An alternative if cgitb is not available is to do the following:

    import sys
    sys.stderr = sys.stdout
    
  6. On the server the python script permissions must be set to execute. After uploading your files be sure to edit the first line and set the permissions for the file to execute.

检查是否可以直接运行Python脚本。如果不能,请按照上述步骤(2-6)进行修复。然后当Python脚本正常工作时,调试shtml。


谢谢您的好回答,即使有点晚了。 :) +1,已标记为已解决。 - Niklas R

0

使用

print("hello world")

而不是

print "hello world"

否则你将在Python 3.+版本中遇到问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接