0

Having just upgraded a PC to Windows 10, a strange intermittent error has appeared when running a Python script as cgi under Apache. The particular script shows a list of available Python modules by calling help("modules"). Sometimes the script executes fine, but other times it fails. When it fails, the browser shows "Internal Server Error" and the Apache error error log has the relatively uninformative error message "End of script output before headers".

Experiments show that the error occurs about 5 times out of every 20 attempts.

Curiously, the script always executed fine when the same PC used Windows 7, and the same script executes fine (as cgi under Apache) when run on an Ubuntu server. When run directly as a Python script (no cgi/Apache), it works fine even under Windows 10. The problem has only been seen with Windows 10 and Apache.

The full script, called testX.py, is:

#!/usr/bin/python
# Note the path to Python may vary
#  
print "Content-type: text/html\n"
print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
print "<HTML>"
print "<HEAD>"
print "<TITLE>Python Test</TITLE>"
print "</HEAD>"
print "<H1>Python Test Script - Available Modules</H1>"
print "<P><PRE>"
help("modules")
print "</PRE></P>"
print "</HTML>"

Technical notes:

  • The script is saved in a cgi-bin directory served through Apache and viewed using a browser pointing to mydomain.com/cgi-bin/testX.py
  • The path to Python is irrelevant on Windows as Apache was given the directive ScriptInterpreterSource Registry
  • If the line help("modules") is removed from the script, it always executes ok
  • Calling help("modules") from the Python command line also works fine
  • Apache version 2.4.33 (32 bit)
  • Python version 2.7.14
  • Windows 10 Pro (10.0.17134.81)

It took a while to isolate the problem to the combination of help("modules") and Windows 10, but does anyone know why this is a problem or why it is intermittent?

UPDATE

There was some talk in previous years that help("modules") could cause a segmentation fault, which would be consistent with the behaviour seen here. See Using help and pydoc to list python modules not working

I've replaced the doubtful call to help("modules") with a simple function which outputs an HTML table using pkgutil:

def help_modules():
    from pkgutil import iter_modules
    mod_list = []
    for thismod in iter_modules():
        mod_list.append(thismod[1])
    mod_list.sort()
    nummods = len(mod_list)
    numcols = 6
    numblanks = (-nummods) % numcols
    mod_list.extend([''] * numblanks)
    numrows = len(mod_list) / numcols
    print "<TABLE style='width:100%; table-layout: fixed;'>"
    for i in range(0,numrows):
         print "<TR>"
         for j in range(0,numcols):
             print "<TD>%s" % (mod_list[i+j*numrows])
         print "</TR>"
    print "</TABLE>"    
    return

Everything seems fine now.

Martin Baxter
  • 109
  • 2
  • 6

0 Answers0