【问题标题】:Python file contents printed when called from HTML instead of result从 HTML 调用而不是结果时打印的 Python 文件内容
【发布时间】:2017-08-30 21:31:28
【问题描述】:

我在 Windows 10 的 C:\Apache24\htdocs 目录下有两个文件 index.html 和 form.py。内容如下:

index.html

<html>  
<head>  
<title>demo web file python2.7</title>  
</head>  
<body>  
<form name="pyform" method="POST" action="/form.py">  
  <input type="text" name="fname" />  
  <input type="submit" name="submit" value="Submit" />  
</form>  
</body>  
</html>   

form.py

#!C:\Python27/python
print "Content-Type: text/html"      

import cgi,cgitb  
cgitb.enable() #for debugging  
form = cgi.FieldStorage()  
name = form.getvalue('fname')  
print "Name of the user is:",name  

Apache 服务器配置为使用以下设置运行 cgi 脚本:

DocumentRoot "c:/Apache24/htdocs"
<Directory "c:/Apache24/htdocs">
    Options Indexes FollowSymlinks ExecCGI
    AllowOverride All
    Require all granted
</Directory>

AddHandler cgi-script .cgi .py

当我填充文本区域并单击页面上的“提交”按钮时,我会在结果页面上获得 python 文件的全部内容,而不是我放入框中的内容。

缺少什么或我做错了什么?非常感谢。

【问题讨论】:

  • 我在print('Content-Type... 之后缺少新行,但这与您的问题无关。 Apache 不接受 form.py 作为 CGI,因此不是 executing 而是显示文件的内容。

标签: python html cgi


【解决方案1】:

您需要使用ScriptAlias 指令将htdocs 标记为留给CGI 程序使用。

ScriptAlias "C:/Apache24/htdocs"

参考资料:

【讨论】:

  • 这并没有改变任何东西。我允许访问我的根目录,因此我不需要为另一个目录设置别名。
  • 是的,您需要 ScriptAlias 任何包含您想要在其上执行 CGI 的文件的目录,除了 DocumentRoo。是否加载了 CGI 模块?加载模块cgi_module modules/mod_cgi.so
  • 我没有DocumentRoot以外的任何目录,这只是为了开发。模块已加载。
猜你喜欢
  • 2016-03-31
  • 2014-04-23
  • 2020-06-11
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多