如何执行Python CGI脚本? [英] How to execute Python CGI Script?

查看:96
本文介绍了如何执行Python CGI脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 .shtml
文件中执行Python CGI脚本,但是我不知道如何执行。我已经找到几种方法了,但是似乎没有任何效果。而且要找到真正可以显示如何执行脚本而不是编写脚本的东西要困难得多! ; /

I want to execute a Python CGI Script within a .shtml file, but I just can't figure out how. I already found several ways?, but nothing seemed to work. And there it was a lot harder to find something that actually shows how to execute a script, and not how to write one ! ;/

我的HTML:
http:// pastebin .com / 4sNZTZNQ

和我的脚本:
http://pastebin.com/w5vGXCBp

我对CGI和任何Webstuff都是陌生的,但我使用Python进行了一半以上的编程

I'm very new to CGI and any Webstuff, but I'm programming with Python over half an year now.

PS:很抱歉,代码的格式令人困惑,我现在将其上传到pastebin上:S

PS: Sorry for the confusing formatting of the code, I now uploadednit onto pastebin :S

// edit:
好​​吧,现在有了更多信息,因为它仍然无法工作。

//edit: Ok, now some more information, because it still does not work.

从heliohost.org上,我被定向到 http://area52.heliohost.org/cgi-bin/snakecharmer.py 可以找到python解释器的路径。

From heliohost.org, i was directed to http://area52.heliohost.org/cgi-bin/snakecharmer.py where you can find the paths to python interpreters.

这是我的文件夹结构:

-public_html
-.htaccess
- main.py
-index.shtml

This is my Folder Structure:
- public_html - .htaccess - main.py - index.shtml

.htaccess的内容:

Content of .htaccess:

Options +ExecCGI
AddHandler cgi-script .py

main.py的内容:

Content of main.py:

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

print "Hello World!"

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

Content of index.shtml: http://pastebin.com/Trg8sXBc

现在,单击链接,出现 500 InternalServerError出现,我不明白为什么。 :(

Now, clicking on the link, an "500 InternalServerError" Appears, and I don't understand why. :(

服务器错误日志只是这样说:

The Server error-log just says this:

[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/


推荐答案

这是我前一段时间写的东西。

Here is something I wrote up a while ago.

有一些使Python在CGI中运行的提示。


  1. 始终通过Apache浏览页面
    请注意,通过浏览器查看文件系统中的文件对html页面上的大多数内容有效,但对CGI而言则不起作用。要使脚本正常工作,必须通过htdocs打开文件系统。浏览器的地址行应如下所示:

  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

如果您通过以下方式打开文件文件系统,CGI将无法运行。例如,这在您的位置栏中浏览器:

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)


  • 将脚本行尾转换为Unix格式:
    大多数编辑器都可以选择显示行末,然后是从Unix转换为PC格式的工具。您必须将行尾设置为Unix格式。

  • 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.

    在CGI脚本的第一行上声明Python解释器的路径:
    您必须具有以下任一行作为Python CGI脚本的第一行:

    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
    

    使用顶行当您在PC上调试时,底部位于服务器(例如1and1)上。我将这些行保留为如图所示,然后在删除服务器上的第一行后通过删除第一行对其进行编辑。

    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.

    在打印任何内容之前打印指定HTML的内容类型其他输出:
    这可以通过在脚本的很早的位置添加以下行来完成:

    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"
    

    请注意,第2行是

    设置Python脚本以提供调试信息:
    导入以下内容以获取详细的调试信息。

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

    import cgitb; cgitb.enable()
    

    如果cgitb不可用,请执行以下操作:

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

    import sys
    sys.stderr = sys.stdout
    


  • 在服务器上,必须将python脚本权限设置为要执行。
    上传文件后,请务必编辑第一行并设置文件执行权限。

  • 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。

    Check to see if you can hit the python script directly. If you can't, fix with the above steps (2-6). Then when the Python script is working, debug the shtml.

    这篇关于如何执行Python CGI脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆