繁体   English   中英

在Windows上的Apache中执行Python cgi文件

[英]Executing Python cgi files in Apache on Windows

我正在尝试使用Apache运行一个非常简单的“ Hello World”程序。 但是,Apache在尝试执行我的python文件时返回500 Internal Server Error。 我在这里阅读了几个类似的主题,并尝试了建议,但没有走运。 我尝试过的事情:

  1. 将带有.py文件的AddHandler包含到.conf文件中
  2. 将ExecCGI添加到.conf中的“选项索引”行。
  3. 确保第一件事输出是带有两个行尾字符的““ Content-Type:text / html”。
  4. 在python文件的顶部添加一个shebang行,以定向到python解释器。 我不确定我是否做对了。
  5. 重新启动Apache

我使用的工具包括:

  • Windows 7的
  • Python 3.5
  • 阿帕奇2.4

我的代码:HTML文件(在Apache文件夹的htdocs文件夹中):

<form action="/cgi-bin/hello_get.py" method="post">
First Name: <input type="text" name="first_name">  <br />

Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
</form>

python文件(在cgi-bin文件夹中):

# Note that I tried this without the C:/ also
#!C:/Users/MyName/workspace/Flask/flask/Scripts

# Import modules for CGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
first_name = form.getvalue('first_name')
last_name  = form.getvalue('last_name')

print("Content-Type:text/html\r\n\r\n")
print("<html>")
print("<head>")
print("<title>Hello - Second CGI Program</title>")
print("</head>")
print("<body>")
print("<h2>Hello %s %s</h2>" % (first_name, last_name))
print("</body>")
print("</html>")

我想到了。

在我的排行中,而不是:

#!C:/Users/MyName/workspace/Flask/flask/Scripts

我应该:

#!C:/Users/MyName/workspace/Flask/flask/Scripts/python.exe

我以为我的shebang应该有一条通往python解释器的路径,但我没有意识到我需要解释器的实际完整路径。

现在正在工作。

综上所述,如果按照以下说明操作后出现此问题: http : //editrocket.com/articles/python_apache_windows.html确保使用Windows时,该路径是从C:/驱动器到python.exe可执行文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM