使用Xampp运行Python脚本

时间:2017-03-09 20:31:55

标签: python apache xampp

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

我做了什么:

编辑过httpd.conf

AddHandler cgi-script .cgi .pl .asp .py  

在我的所有脚本的顶部,我添加了这个:

#!j:/Installeds/Python/python   
print "Content-type: text/html\n\n"

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

Obs:我认为这可能会有所帮助> Apache>Error.log

  

[cgi:error] [pid 6364:tid 1620](9)错误的文件描述符:[client :: 1:51083] AH01222:不知道如何生成子进程:C:/ Files和Installs / Xampp /htdocs/Test/main.py
  AH02102:C:/ Files和Installs / Xampp / htdocs / Test / main.py不可执行;确保解释的脚本有“#!”要么 ”'!”第一行

4 个答案:

答案 0 :(得分:22)

在xampp中为Windows运行Python:

第1步:[下载Python]

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

第2步:[安装Python]     安装在硬盘的任何目录中     [前。 d:\蟒-3.6.2]

第3步:[Configur Python]     打开安装xammp的目录     转到apache>> CONF     [前。 d:\ XAMPP \阿帕奇\ CONF \的httpd.conf]     您将看到名为httpd.conf的文件     在任何文本编辑器中打开它&将以下代码放在该文件的末尾

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

第4步:[可选]

在同一文件中搜索     当你发现它最终放入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>

第5步:[重启apache / xampp]

这就是编辑,现在从你的xampp控制面板重启apache

第6步:[从xammp运行Python]

打开文本编辑器&amp;现在在xammp htdoc目录下测试python [ex。 d:\ XAMPP \ htdocs中\ PythonProject。     但是在脚本开头等待,您需要指定安装python的路径。在我的情况下它的D:/python-3.6.2/python.exe。在你的情况下它可能会有所不同,这取决于你安装的版本python&amp;您的硬盘驱动器python代码的目录。

    #!D:/python-3.6.2/python.exe
    print("Content-Type: text/html\n")
    print ("Hello Python Web Browser!! This is cool!!")

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

将文件保存为htdocs&amp;中的test.py。打开http://localhost/PythonProject \ test.py。如果一切顺利,你会看到文本“Hello Python Web Browser !!这很酷!!”

答案 1 :(得分:2)

我正在运行ubuntu 16.04,所以我的回答可能有所不同。我在/ opt / lampp / htdocs / PythonProject中使用带有chrome 3文件的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!!")

我在/opt/lampp/etc/httpd.conf中编辑了httpd.conf文件,但没有添加

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

在文件末尾,而是在现有行的末尾添加了 .py

AddHandler cgi-script .cgi .pl

最后,我将文件设置为chmod +x /opt/lampp/htdocs/PythonProject/test.py可执行文件,然后通过浏览器运行了该文件:

http://localhost/PythonProject/test.py

输出:

hello world!

I can view this in my browser yay!!

答案 2 :(得分:0)

错误文件描述符表示文件已损坏,并且表示无法运行脚本,因此您可能错误地设置了python。

答案 3 :(得分:0)

  1. 从此处(https://www.python.org/downloads/)下载python并安装
  2. 打开XAMPP控制面板,单击配置并转到httpd.conf文件>>搜索addhandler并添加“ .py”(不带引号),就像在屏幕快照中一样(如果未添加) httpd.conf file
  3. 重新启动apache服务器

要运行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>")

在第一行中,您必须在放入shebang后输入python.exe文件的位置(#!) “用户名”-您的PC的用户名 每个用户的不同。您可以从环境变量中找到python的位置(请参见下面的屏幕截图)

py environment variables

  • 然后将脚本放入xampp >> htdocs文件夹中
  • 打开浏览器,然后键入localhost /“ filename” .py(http://localhost/filename.py) [“文件名” =脚本名] 您会看到此输出

output

相关问题