使用Xampp运行Python脚本

2024-09-28 22:25:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用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:我认为这可能有帮助

[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


Tags: and代码pytest编辑mainconf浏览器
3条回答

在xampp for windows中运行Python:

第一步:[下载Python]

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

步骤2:[安装Python] 在硬盘的任何目录中安装 [例如D:\ python-3.6.2]

步骤3:[配置Python] XAMPP GUI可以快速访问httpd.conf文件,如下所示: enter image description here

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

AddHandler cgi-script .py
ScriptInterpreterSource Registry-Strict

步骤4:[可选]

在同一个文件中搜索<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>

步骤5:[重新启动apache/xampp]

只需编辑,现在从xampp控制面板重新启动apache

步骤6:[从xammp运行Python]

现在在xammp htdoc目录[ex.D:\ xampp\htdocs\PythonProject]上打开文本编辑器&test python。 但是在脚本的开头,您需要指定安装python的路径。在我的例子中是D:/python-3.6.2/python.exe,在您的例子中可能会有所不同,这取决于您安装python的版本以及硬盘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&;openhttp://localhost/PythonProject\test.py中的test.py。如果一切顺利,您将看到文本“Hello Python Web Browser!!太酷了!!”

我在运行ubuntu 16.04,所以我的答案可能有点不同。我正在使用google chrome浏览器和python 3文件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!!")

我在/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!!

错误的文件描述符意味着一个文件已损坏,并且它说它无法运行脚本,因此您可能错误地设置了python。

相关问题 更多 >