0

I have a very basic Python script being run as CGI, I am able to run it with only print statements and they execute and display as HTML. When I try to do any file system calls such as using logging, making files or using subprocesses I get an error 500.

#!/usr/bin/python
import cgitb; cgitb.enable()
import cgi
import sys
from subprocess import call

call('ls') #Error here, if I remove I get no error 500 and see "Hello"
form = cgi.FieldStorage()


print "Content-Type: text/html"
print
print "<html><body>Hello"
print "</body></html>"

The CGI script has execute permission:

chmod a+x example.cgi

In my Apache configuration I have the virtual hosts to execute CGI setup:

<Directory "/path/to/cgi/cgi-bin/">
    AddHandler cgi-script cgi pl
    Options +FollowSymLinks +ExecCGI
    AllowOverride None  
</Directory>

And setup the CGI module for Apache:

sudo a2enmod cgi

If I run the cgi script directly on the server, all the file manipulation works perfectly, if I try to access it from the browser, i.e example.com/test.cgi it gives me the 500 error. I also ran dos2unix to make sure it's formatted correctly.

Michael
  • 23
  • 1
  • 3
  • Try to print out what the error is https://stackoverflow.com/questions/24849998/how-to-catch-exception-output-from-python-subprocess-check-output – Adelina Jul 23 '18 at 17:58
  • I cannot print out the error because it will work fine calling the script on the server, and I cannot log the error from the browser because any logging results in the same error – Michael Jul 23 '18 at 18:06

1 Answers1

0

Try to use os.system('ls') instead a call, or use it before you enabled the cgitb.

T. Graim
  • 67
  • 4