How to run python scripts in XAMPP on mac? - python

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!")

Related

Xampp not running 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

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?

Setting up Python in AMPPS - Internal Server Error

I'm trying to use Python with AMPPS, which is an Apache / MySQL / etc. bundle for Windows. There's just one problem though; every script I try to run returns an 'Internal Server Error', even though it's correct code.
This is probably an issue that's not worth posting it here, but I googled for three hours and I just can't find any help.
My httpd.conf is over here: http://pastebin.com/5DMU2cUU
The single line of code I use is: print "Hello World!";
Notepad++ is set to EOL Conversion->UNIX format (before you ask).
This is all I know so far. Please don't hate me for posting this (I know you love to do that), I'm new and confused :3
mod_wsgi is already configured in AMPPS. You can see it in loaded modules of apache.
httpd.exe -D DUMP_MODULES
You just need to configure your script now.
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
You can modify the python.conf included in httpd.conf too. i.e for using mod_wsgi directives.
This is official hello world example from mod_wsgi. Hope this helps.
Follow below steps:
Open httpd.conf
Append .py to AddHandler cgi-script .cgi .pl under tag
Now AddHandler cgi-script .cgi .pl becomes AddHandler cgi-script .cgi .pl .py
Search for block and Add ExecCGI to Options FollowSymLinks Indexes.
Now its become Options FollowSymLinks Indexes ExecCGI
Save the httpd.conf file, stop the apache server and restart again.
If the apache server restarts without any problem process you have successfully setup ampps to run python, otherwise restore the conf file from backup. In ampps there is an option to restore default configuration.
Add python exe file link line to the top of the python file to direct to the Python interpreter.It might be #!C:\Ampps\python\python.exe
Add this line print("Content-type: text/html\n")
Add this line print("Hello world!")
Now execute your python file. http://127.0.0.1/test/test.py
wsgi_module must be installed for apache to run python script via execCGI. On ampps this is installed and added to conf file by default.
link:http://www.lakshman.com.np/run-python-on-ampps-windows/

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