使用 Xampp 运行 Python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42704846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:02:51  来源:igfitidea点击:

Running Python scripts with Xampp

pythonapachexampp

提问by PlayHardGoPro

I'm using python 2.7.13
At first the browser was showing the raw code.

我正在使用 python 2.7.13
起初浏览器显示原始代码。

what I did:

我做了什么:

Edited httpd.conf

编辑 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.

现在它给了我Internal Server Error (500),我不知道还能尝试什么......第一次使用 python。

Obs: I think this may help> Apache>Error.log

Obs:我认为这可能会有所帮助> 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

[cgi:error] [pid 6364:tid 1620] (9)Bad file descriptor: [client ::1:51083] AH01222: 不知​​道如何生成子进程:C:/Files and Installs/Xampp/htdocs/ Test/main.py
AH02102: C:/Files and Installs/Xampp/htdocs/Test/main.py 不可执行;确保解释的脚本有“#!” 或者 ”'!” 第一行

回答by matinict

Run Python in xampp for windows:

在 xampp 中为 Windows 运行 Python:

STEP-1:[Download Python]

STEP-1:[下载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]

从 www.python.org 下载并安装最新版本的 python 下载 Python 并单击任何版本的 Windows 安装程序 [例如。python-3.6.2]

STEP 2: [Install Python]Install in any directory of your harddrive [ex. D:\python-3.6.2]

第 2 步:[安装 Python]安装在硬盘的任何目录中 [例如。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

第 3:[配置 Python]XAMPP GUI 可以像这样快速访问 httpd.conf 文件: 在此处输入图片说明

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:

否则打开安装 xammp 的目录转到 apache >> conf e.g.) D:\xampp\apache\conf\httpd.conf。你会看到一个名为httpd.conf. 在任何文本编辑器中打开它并将以下代码放在该文件的末尾:

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

STEP 4:[optional]

第 4 步:[可选]

In same file search for <IfModule dir_module>. When you've found it put index.pyin the end It will look something like this

在同一个文件中搜索<IfModule dir_module>. 当你发现它放在index.py最后它看起来像这样

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

第 5 步:[重启 apache/xampp]

That's all for editing, now restart apache from your xampp control panel

这就是编辑的全部内容,现在从您的 xampp 控制面板重新启动 apache

STEP 6:[Run Python from xammp]

第 6 步:[从 xammp 运行 Python]

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 .

现在在 xammp htdoc 目录中打开文本编辑器并测试 python [例如。D:\xampp\htdocs\PythonProject]。但是在脚本的开头等待,您需要指定安装 python 的路径。在我的情况下它的 D:/python-3.6.2/python.exe 。在你的情况下它可能会有所不同,这取决于你安装的 python 版本和你的硬盘 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!!"

在 htdocs 中将文件另存为 test.py 并打开http://localhost/PythonProject\test.py。如果一切顺利,您将看到文本“Hello Python Web Browser!!This is cool!!”

回答by wade king

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:

我运行的是 ubuntu 16.04,所以我的回答可能有点不同。我在 /opt/lampp/htdocs/PythonProject 中使用带有名为 test.py 的 python 3 文件的 google chrome 浏览器:

#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 notadd

我编辑我的httpd.conf文件中/opt/lampp/etc/httpd.conf,我并没有添加

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

to the end of the file, instead I added .pyto the end of the existing line

到文件的末尾,而不是我将.py添加到现有行的末尾

AddHandler cgi-script .cgi .pl

finally i made the file executable by chmod +x /opt/lampp/htdocs/PythonProject/test.pyand then I just ran it through my browser:

最后我使文件可执行chmod +x /opt/lampp/htdocs/PythonProject/test.py,然后我只是通过浏览器运行它:

http://localhost/PythonProject/test.py

OUTPUT:

输出:

hello world!

I can view this in my browser yay!!

回答by ahmedshahriar

  1. Download python from here (https://www.python.org/downloads/) and install it
  2. 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
  3. Restart the apache server
  1. 从这里下载 python ( https://www.python.org/downloads/) 并安装它
  2. 打开 XAMPP 控制面板,点击 config 并转到 httpd.conf 文件 >> 搜索 addhandler 并添加“.py”[不带引号],就像截图(如果没有添加) httpd.conf 文件
  3. 重启apache服务器

To run a python script:Open any text editor and put this code

要运行 python 脚本:打开任何文本编辑器并输入此代码

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

在第一行中,您必须在输入 shebang (#!) “用户名”后输入 python.exe 文件的位置——您的 PC 的用户名 这将因用户而异。你可以从环境变量中找到python的位置(见下面的截图)

py environment variables

py环境变量

  • 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
  • 然后将脚本放在 xampp>> htdocs 文件夹中
  • 打开浏览器并输入 localhost/”filename”.py ( http://localhost/filename.py) [ “filename”= script name] 你会看到这个输出

output

输出

回答by Noah Cristino

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.

错误的文件描述符意味着文件已损坏,并表示它无法运行脚本,因此您可能未正确设置 python。