Raoul
Raoul

Reputation: 1892

python CGI failure, succeeding in perl

I am trying to use python CGI scripting on my server. Both the python and perl scripts displayed below are in the same directory, with permission 755.

If I try a python hello world using this code, and point the browser to it:

#!/usr/bin/env python

import cgi
import cgitb
cgitb.enable()

print "Content-type:text/html\r\n\r\n"
print "Hello, World!"

It fails with: Error 500: Premature end of script headers: cgiwrap

I think I have a cgiwrap permission issue, as my server is apache, and I read that the -w option in perl could bypass that kind of issue.

Interestingly, I tried with the perl Hello World provided on the host provider help page (which fails when run as is):

#!/usr/bin/perl

print "Content-type: text/html\r\n\r\n";
print "Hello World";

It fails with: CGIWrap Error: Script Execution Failed, Error Message: No such file or directory

While if I try to execute (only changing the shebang):

#!/usr/bin/perl -wT

print "Content-type: text/html\r\n\r\n";
print "Hello World";

it runs...

Furthermore, the command python index.py in Terminal runs as expected, while ./index.py fails with No such file or directory


The which python command output: /usr/bin/python

Upvotes: 0

Views: 225

Answers (2)

Raoul
Raoul

Reputation: 1892

Finally seems that I had Windows-style end-of-lines... Although I do not understand how that happened, since the files never travelled through a Windows OS.

Upvotes: 0

jsbueno
jsbueno

Reputation: 110291

It could be that the default Python in the system is Python 3, not Python 2. In this case, the print statements will make the script fail with a syntax error.

Can you check the wbe server's logs? Maybe there is a backtrace detailing what went wrong.

Anyway, to be safe from that, place on the second line of your script from __future__ import print_function, and add parenthesis around the strings following the prints.

(btw, the line endings should be ok for a CGI - check HTTP header line break style )

One other possible problem is that your web-server is not configured to run .py files as CGI programs - check its configuration.

Also, this program should work from the terminal, as it is - try to just run it from the terminal, and see if it prints-out the desired output - if not, check the error messages and correct it. You likely have a syntax error in there, either due to the "print" issue, or maybe incorrect indentation, or other thing we can't see in the snippet.

Upvotes: 1

Related Questions