在浏览器中运行.py文件

8 投票
10 回答
18390 浏览
提问于 2025-04-16 21:01

我想在浏览器中运行一个Python文件。
我已经安装了Apache服务器,并配置了httd.conf文件。
接着,我创建了一个test.py文件。
然后我试着在浏览器中通过输入htt://localhost/test.py来运行这个test.py文件。
但是当我这么做的时候,出现了以下错误:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, lohith.pinto@primefocusworld.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.

在我的错误日志中

[Thu Jul 07 18:39:55 2011] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.  : couldn't create child process: 720002: test.py
[Thu Jul 07 18:39:55 2011] [error] [client 127.0.0.1] (OS 2)The system cannot find the file specified.  : couldn't spawn child process: C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/test.py

这可能是什么问题呢?

10 个回答

1

如果这是一个CGI程序,确保它符合CGI的规范。也就是说,它在输出任何其他内容之前,必须先输出一行包含MIME类型的头信息,并且在这行之后要有一个空行。

2

在htdocs文件夹下,使用Options +ExecCgi,

然后设置ScriptAlias /cgi-bin/为"C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/"

这是我现在的设置,运行得很好。

13

如果你的脚本开头的shebang(也就是指明用哪个程序来运行这个脚本的那行)不正确,也可能会出现问题。比如说,如果你用的是Unix系统,shebang可能是这样的:#!/usr/bin/env python。如果是这种情况,你需要把它改成正确的Python可执行文件的路径,比如:#!C:\Python26\python.exe。或者你可以看看这个回答:在Windows上用Apache 2如何忽略Perl的shebang?

撰写回答