I\'m using python 2.7.13
At first the browser was showing the raw code.
what I did:
AddHandler cgi-script .cgi .pl .as
Bad File descriptor means that a file is corrupt and it says it can not run the script so you probably have python incorrectly set up.
To run a python script: Open any text editor and put this code
#!C:/Users/"Username"/AppData/Local/Programs/Python/Python37-32/python.exe
print("content-type: text/html\n\n" )
print("<br><B>hello python</B>")
In the first line, you have to type the location of the python.exe file after putting a shebang (#!) “username” — username of your PC This will differ from one user to another. You can find the python location from environment variables (see the screenshot below)
py environment variables
output
Run Python in xampp for windows:
STEP-1:[Download Python]
Download & install the latest version of python from www.python.org Download Python & click on the windows installer of any version [ex. python-3.6.2]
STEP 2: [Install Python] Install in any directory of your harddrive [ex. D:\python-3.6.2]
STEP 3: [Configure Python] The XAMPP GUI can quickly access the httpd.conf file like so:
Otherwise open the directory where xammp was installed
Go to apache >> conf e.g.) D:\xampp\apache\conf\httpd.conf
.
You'll see a file named httpd.conf
. Open it in any text editor & put the below codes in the end of that file:
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
STEP 4:[optional]
In same file search for <IfModule dir_module>
.
When you've found it put index.py
in the end
It will look something like this
<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm index.py
</IfModule>
STEP 5:[restart apache/xampp]
That's all for editing, now restart apache from your xampp control panel
STEP 6:[Run Python from xammp]
Open a text editor & test python now on xammp htdoc directory [ex. D:\xampp\htdocs\PythonProject]. But wait at the beginning of your script you need to specify the path where you've installed python. In my case its D:/python-3.6.2/python.exe .In your case it may be different, depending up on the version you've installed python & the directory of your hard drive python Code .
#!D:/python-3.6.2/python.exe
print("Content-Type: text/html\n")
print ("Hello Python Web Browser!! This is cool!!")
or
#!C:/Users/YOUR_WINDOWS_PROFILE/AppData/Local/Programs/Python/Python37-32/python.exe
print("Content-Type: text/html")
print()
print ("""
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
""")
Save the file as test.py in htdocs & open http://localhost/PythonProject\test.py
.If everything goes well, You'll see the text "Hello Python Web Browser!! This is cool!!"
Im running ubuntu 16.04 so my answer might be a little different. I am using a google chrome browser with a python 3 file called test.py in /opt/lampp/htdocs/PythonProject:
#test.py
#!/usr/bin/env python3
print('Content-type: text/html\r\n\r')
print("<p>hello world!</p>")
print("I can view this in my browser yay!!")
I edited my httpd.conf file in /opt/lampp/etc/httpd.conf and I did not add
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
to the end of the file, instead I added .py to the end of the existing line
AddHandler cgi-script .cgi .pl
finally i made the file executable by chmod +x /opt/lampp/htdocs/PythonProject/test.py
and then I just ran it through my browser:
http://localhost/PythonProject/test.py
OUTPUT:
hello world!
I can view this in my browser yay!!