Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Python CGI Script?

Tags:

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

My Html: http://pastebin.com/4sNZTZNQ

And my Script: http://pastebin.com/w5vGXCBp

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!"

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

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/
like image 462
Niklas R Avatar asked Jun 14 '11 22:06

Niklas R


1 Answers

Here is something I wrote up a while ago.

There are some tips to getting Python working in 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.

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.

like image 59
gschoep Avatar answered Sep 23 '22 13:09

gschoep