简体   繁体   English

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

[英]Executing Python cgi files in Apache on Windows

I am trying to run a very simple "Hello World" program with Apache. 我正在尝试使用Apache运行一个非常简单的“ Hello World”程序。 However, Apache returns a 500 Internal Server Error when it tries to execute my python file. 但是,Apache在尝试执行我的python文件时返回500 Internal Server Error。 I've read several similar topics on here and tried the suggestions, no luck. 我在这里阅读了几个类似的主题,并尝试了建议,但没有走运。 Things I have tried: 我尝试过的事情:

  1. Including the AddHandler with .py files to the .conf file 将带有.py文件的AddHandler包含到.conf文件中
  2. Adding ExecCGI to the "Options Indexes" line in the .conf. 将ExecCGI添加到.conf中的“选项索引”行。
  3. Making sure the first thing output is ""Content-Type:text/html" with 2 end of line characters. 确保第一件事输出是带有两个行尾字符的““ Content-Type:text / html”。
  4. Adding a shebang line to the top of the python file to direct to the Python interpreter. 在python文件的顶部添加一个shebang行,以定向到python解释器。 I'm not sure if I'm doing this part right. 我不确定我是否做对了。
  5. Restarting Apache 重新启动Apache

The tools I am using include: 我使用的工具包括:

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

My code: The HTML File (in the htdocs folder in the Apache folder): 我的代码: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>

The python file (in the cgi-bin folder): 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>")

I figured it out. 我想到了。

In my shebang line, instead of: 在我的排行中,而不是:

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

I should have: 我应该:

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

I thought my shebang should have a path to where the python interpreter lives, I didn't realize I needed the actual full path of the interpreter. 我以为我的shebang应该有一条通往python解释器的路径,但我没有意识到我需要解释器的实际完整路径。

It is working now. 现在正在工作。

So to recap, if you are having this issue after following these instructions: http://editrocket.com/articles/python_apache_windows.html Make sure that if you are using Windows the path is the full absolute path from the C:/ drive to the python.exe executable. 综上所述,如果按照以下说明操作后出现此问题: 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