Xampp not running python - python

I am using Xampp 7.2.6-0 on ubuntu 16.04 with Google Chrome 64.0.3282.186 (Official Build) (64-bit). I'm trying to run a python script through my browser. I have already edited the httpd.conf file in /opt/lampp/etc and appended the following lines.
#edit by Wade King
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
My python script is called docxReader.py and is in /opt/lampp/htdocs/PyFiles/XampPython/docxReader.py
It looks like this and compiles with no errors:
#!/usr/bin/env python3
import cgitb
cgitb.enable()
print("Content-Type: text/html")
print("")
print("If you can read this inside a Web-Browser window, you can successfully run Python through XAMPP!'")
when I type http://localhost/PyFiles/XampPython/docxReader.py in my browser the result is the text of docxReader.py file like so:
#!/usr/bin/env python3
import cgitb
cgitb.enable()
print("Content-Type: text/html")
print("")
print("If you can read this inside a Web-Browser window, you can successfully run Python through XAMPP!'")
I was expecting the page to display the output of my python file, not the text of the file itself. What is going on here, how do I configure Xampp to display the output of my python file?

never mind I figured it out, I removed the
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
lines that I added to the end of the httpd.conf
then i found the line in httpd.conf:
AddHandler cgi-script .cgi .pl
and appended .py to the line
then i just ran chmod +x docxReader.py to make it executable and opened the file in my browser and it seems to work

Related

500 internal Server error when configuring python cgi

I have been trying to run a simple python script hello.py with CGI but am getting 500 Internal Server Error.
My python code.
#!/usr/bin/python2.7
print '<html>'
print '<head>'
print '<title>Hello World - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! This is my first CGI program</h2>'
print '</body>'
print '</html>'
The directory which i have the python script running is in /var/www/crunchworld.The conf file which i enabled is in `/etc/apache2/conf-available/crunchworld.conf
The conf file looks like
<Directory /var/www/crunchworld>
Options +ExecCGI
AddHandler cgi-script .cgi
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have cgi enabled and given the necessary permission for the file hello.py but its still showing me internal server error.When i checked the logs i see
End of script output before headers: hello.py
I have researched about the error and give appropriate permissions for the file but it doesnt work.
Any help would be so much appreciated. Thanks in advance.
Further changes i have made.
I have added AddHandler cgi-script .cgi .py in my crunchworld.conf file.
2.I have given permission for the file hello.py
I have symlinked /etc/apache2/conf-available/crunchworld.conf in /etc/apache2/conf-enabled
4.I had already installed python2.7 on the path /usr/bin/python2.7 and i have also tried using #!/usr/bin/env python but still it doesnt work.
Upon checking the logs i found End of script output before headers: hello.py, referer: http://localhost/
Thank you for your recommendations but it is still showing 500 internal error.
Your CGI script must also output header information.
The minimum required is the Content-type header -- which in this case should be set to text/html.
Add this to the start of your CGI script (before you print anything else).
print 'Content-type: text/html\n'
Take note of the extra trailing newline -- its necessary to leave at least one blank line between the header(s) and the content itself.
Update:
For further troubleshooting, do the following:
Make sure your CGI script has the correct permissions set: chmod 0755 hello.py just to be sure.
Your script appears to be .py whereas your apache config appears to only specify .cgi files. Your AddHandler should be AddHandler cgi-script .cgi .py.
You should symlink your /etc/apache2/conf-available/crunchworld.conf file in /etc/apache2/conf-enabled if you haven't already done so. Do so by running the following: cd /etc/apache2/conf-enabled; sudo ln -s ../conf-available/crunchworld.conf.
Always remember to restart apache if you make any changes to your apache config: e.g. sudo service apache2 restart.
Check that your hashbang line is correct. Does /usr/bin/python2.7 exist? You can try setting instead to #!/usr/bin/env python or #!/usr/bin/env python2. (Or better yet, switch to python3 if possible on your system).
Check the apache error logs once again. E.g. tail -20 /var/log/apache2/error.log (or wherever your logs are).
You can attempt further debugging with the cgitb module (see https://docs.python.org/3/library/cgitb.html).

How to run python scripts in XAMPP on mac?

I have a ajax call being make to a python script.But instead of executing the code the call just prints the raw code.
I'm using xampp installed on mac.
What I did:
[edited the following line in httpd.conf]
AddHandler cgi-script .cgi .pl .asp .py
My python script present in htdocs is:
#/usr/local/opt/python/bin/python3.7
print("Hello World!")

Running Python scripts with Xampp

I'm using python 2.7.13
At first the browser was showing the raw code.
what I did:
Edited httpd.conf
AddHandler cgi-script .cgi .pl .asp .py
At the top of all my scripts I added this:
#!j:/Installeds/Python/python
print "Content-type: text/html\n\n"
Now it's giving me Internal Server Error (500) and I have no idea what else to try... First time with python.
Obs: I think this may help> Apache>Error.log
[cgi:error] [pid 6364:tid 1620] (9)Bad file descriptor: [client ::1:51083] AH01222: don't know how to spawn child process: C:/Files and Installs/Xampp/htdocs/Test/main.py
AH02102: C:/Files and Installs/Xampp/htdocs/Test/main.py is not executable; ensure interpreted scripts have "#!" or "'!" first line
Run Python in XAMPP for Windows
Step 1: Download and Install Python
Download and install the latest version of Python from https://www.python.org/downloads.
Step 2: Configure XAMPP for Python
Open the Apache httpd.conf configuration file located at .../xampp/apache/conf/httpd.conf using a text editor of your choice.
The XAMPP GUI can also quickly access the httpd.conf file:
Copy and paste the following code at the end of the file:
AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict
Step 2.5: Add Python extension to default page locations (Optional)
Inside the httpd.conf file search for <IfModule dir_module> to add index.py among others to the list of default page locations.
<IfModule dir_module>
DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm index.py \
default.php default.pl default.cgi default.asp default.shtml default.html default.htm default.py \
home.php home.pl home.cgi home.asp home.shtml home.html home.htm home.py
</IfModule>
Step 3: Restart Apache / XAMPP
If Apache was running while editing, now is the time to restart it.
Step 4: Run Python from XAMPP
Create a folder and Python file in the XAMPP htdocs directory; e.g. .../xampp/htdocs/PythonProject/test.py.
At the beginning of your script, you first need to specify the directory of your Python executable. The default location of Python 3.10.0 is C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe, but in your case, it may be different, depending on the version and directory in which you've installed Python.
#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
After that, you can create your Python script.
#! C:/Users/<YOUR_WINDOWS_PROFILE>/AppData/Local/Programs/Python/Python310/python.exe
print("Content-Type: text/html\n")
print("Hello, World!")
Save the file and open localhost/PythonProject/test.py in your web browser. Your Python script should be running.
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!!
Download python from here (https://www.python.org/downloads/) and install it
Open XAMPP control panel, click on config and go to httpd.conf file >> search for addhandler and add “.py” [without quotation ] just like in the screenshot (if it’s not added)
httpd.conf file
Restart the apache server
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
Then put the script in xampp>> htdocs folder
Open your browser and type localhost/”filename”.py (http://localhost/filename.py)
[ “filename”= script name]
You’ll see this output
output
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.

Text of python scripts is shown in browser instead or running them

I have apache2 running on my ubuntu 14.04. The configuration for the virtual host is
`ScriptAlias /cgi-bin/ /home/(path to some myfolder)/cgi-bin/
<Directory "/home/(path to some myfolder)/cgi-bin">
DirectoryIndex index.py
Options +ExecCGI
AddHandler cgi-script .py
Require all granted
</Directory>`
Then index.py is not running as well as any other python script. Instead the text of the scripts is shown in the browser window.
I also tried to stop apache and use python -m CGIHTTPServer instead, but with the same result.
Also, the command chmod +x index.py was routinely applied to all python scripts I tried.
To run the script I used the url: 127.0.0.1/index.py, though I also tried
127.0.0.1:80/index.py and 127.0.0.1:80/(the whole pathe to the file in the cgi-bin folder)
I use port 80.
The script I use to test is
#!/usr/bin/python2.7
print "Content-type: text/html"
print "Hello, World."
python2.7 is in the folder /usr/bin so the path is correct
The best I found on the Internet were advices implemented under Directory tag above. Bwt, the result does not change if I launch the scripts in browsers as root. I tried Firefox and Chrome, all the same.
The question is how to make my python scripts run in a browser?

Execute Python CGI from /cgi-bin/ folder

I have a standard Apache2 installation on an Ubuntu server. The default settings use ScriptAlias to refer /cgi-bin/ to /usr/lib/cgi-bin/. If I place my Python CGI script in /usr/lib/cgi-bin/ it executes.
I created /var/www/cgi-bin/ with appropriate permissions, removed the ScriptAlias line, changed the Directory entry in the default file for the site, moved the CGI file to /var/www/cgi-bin/ and restarted Apache2, but I was not able to get the script to run. It was appearing as a text file in the browser instead of being executed. The HTML file calling the script refers to /cgi-bin/SCRIPT, so I left that unchanged. I tried variations on /cgi-bin and /var/www/cgi-bin in the config files without success. How can I get a Python CGI file to run from /var/www/cgi-bin?
If you removed the ScriptAlias directive, you should probably add the following lines into the main config for the directory or into .htaccess file:
Options +ExecCGI
AddHandler cgi-script py
please make sure:
the file you are calling itself has enough permission
the file you are calling has an extension that is is in the .htaccess file (see newtover's answer)
specify how you want to run the script in the first line of the file. If you are calling foo.pl, make sure #!/usr/bin/perl is in the first line.

Categories