XAMPP apache setup on Windows to run .py Python files : ScriptInterpreterSource Registry-Strict

1

I've got my installation of the newest XAMPP version (w/PHP 7.2.2) setup and running apache just fine on my Windows 10 laptop for my PHP programming class. I was able to get CGI working to run .py files correctly in the browser window, using the option +ExecCGI and adding .py to the AddHandler line in the /XAMPP/apache/conf/httpd.conf.

Normally the first two lines need to look like this with CGI in order for the .py file to run correctly:

#!C:/Python/Python36_x86/python.exe
print("Content-Type: text/html\n")

However, after reading Apache2.4 documentation for ScriptInterpreterSource: https://httpd.apache.org/docs/2.4/mod/core.html#scriptinterpretersource

I added the correct registry key mentioned in the documentation along with the line:

ScriptInterpreterSource Registry-Strict

to my /XAMPP/apache/conf/httpd.conf

and was able to run my Python script without those first two lines. However, it still requires an extra print() statement on the first line (or print('\n') ).

===========================================================================

MY QUESTION:

Is there any way at all to get around needing the print/newline statement on the first line of the Python script?

Maleko48

Posted 2018-03-12T20:34:08.270

Reputation: 11

Answers

0

Is there any way at all to get around needing the print/newline statement on the first line of the Python script?

Probably not... at least not for using Python via CGI.

For a given CGI script (not just those in Python), the language-equivalent of this "blank line" (CRLF) is needed at the start of any data returned to the browser. And while Apache grabs the interpreter path and value for the Content-type header from the registry, it appears not to include this line.

It still requires an extra print() statement on the first line (or print('\n') ).

For clarity, in case there is any confusion, it simply has to be the first line of text you output (if any) from your script e.g.:

# Print our Python version

import sys

version = sys.version_info
full_version = str(version.major) + '.' + str(version.minor) + '.' + str(version.micro)

# 8000 more lines that don't include print()...

# === Our first line(s) of text output ===
# print ('Content-type: text/html')
print ('')

print ('Python Version: ', full_version)

Note (for everyone else): If you don't have everything registered like OP, you still need a hash-bang such as !# python as your first line and should uncomment print ('Content-type: text/html').


Anaksunaman

Posted 2018-03-12T20:34:08.270

Reputation: 9 278

Thank you for your answer, but I am not allowed to mark it or anything since I am a new member with <15 rep. – Maleko48 – 2018-03-13T17:27:48.580

No worries. You're welcome. =) – Anaksunaman – 2018-03-13T17:43:52.030