0

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

Raoul
  • 1,872
  • 3
  • 26
  • 48
  • Having no experience with cgi, I wonder if you need to explicitly print the `` and `` tags before rendering any actual content. – Kevin Jan 22 '15 at 17:40
  • 1
    Maybe you're using the wrong type of newline when printing the blank line after "Content-type". Try `print "Content-type: text/html\r\n\r\n"` – Kevin Jan 22 '15 at 17:50
  • @BhargavRao I tried, yes. Maybe I am doing it wrong? I edited to try all of your suggestions. BTW, thanks to all for the help, it's really nice of you! – Raoul Jan 22 '15 at 18:06

2 Answers2

1

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.

Community
  • 1
  • 1
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Thanks for the input. I tried `from __future__ import print_function`. Unfortunately, it does not change the result. I checked, the server does handle `.py`. When run in the terminal, it outputs as expected, with no errors. That is what is killing me, because it means that the interpreter is found and executes the script! – Raoul Jan 22 '15 at 19:55
0

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.

Raoul
  • 1,872
  • 3
  • 26
  • 48