简体   繁体   中英

Internal Server Error 500 - Python, CGI

My.py file executes ok in terminal, but gives this error in the browser

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
...
...

Here is the.py file:

#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print "Content-Type: text/html\n\n"     # HTML is following
print                                   # blank line, end of headers

print "<TITLE>CGI script output</TITLE>"
print "<H1>This is my first CGI script</H1>"
print "Hello, world!"

Should i be saving this as a.cgi file? I have tried with the same errors, i have tried many files like this and none work, i am sure the apache server is working as there are other.cgi scripts running from the same directory without issues.

I have also tried: #!/usr/local/bin/python & #!/usr/bin/local/python

Any help appreciated.

EDIT error log output: (2) No such file or directory: exec of '.../.../.../test.py' failed Premature end of script headers: test.py

Here is something I wrote up a while ago. These are some good things to look for when troubleshooting Python CGI.

There are some tips to getting Python working in CGI.

Apache setup: This may be old

Add python as a CGI by modifying the following in the configuration:

    Options Indexes FollowSymLinks ExecCGI
    AddHandler cgi-script .cgi .py
  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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM