Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

apache - 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

"

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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: enter image description here

Otherwise open the directory where xammp was installed Go to apache >> conf e.g.) D:xamppapacheconfhttpd.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:xampphtdocsPythonProject]. 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
")
    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/PythonProjectest.py .If everything goes well, You'll see the text "Hello Python Web Browser!! This is cool!!"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...