python cgi root运行,如何执行Python CGI脚本?

博主在尝试在.shtml文件中执行Python CGI脚本时遇到困难,已经尝试了多种方法但未成功。问题表现为点击链接后出现'500 Internal Server Error'。错误日志显示文件不存在。解决方案包括:确保通过Apache浏览页面,将脚本的换行符转换为Unix格式,指定Python解释器路径,打印HTML内容类型,设置脚本权限为可执行,并在服务器上进行调试。
摘要由CSDN通过智能技术生成

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 ! ;/

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

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

//edit:

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

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

This is my Folder Structure:

- public_html

- .htaccess

- main.py

- index.shtml

Content of .htaccess:

Options +ExecCGI

AddHandler cgi-script .py

Content of main.py:

#! /usr/local/bin/python

print "Content-Type: text/html"

print

print "Hello World!"

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.

There are some tips to getting Python working in CGI.

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)

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.

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.

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.

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

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值