【问题标题】:HTML forms not working with pythonHTML 表单不适用于 python
【发布时间】:2010-11-21 13:54:27
【问题描述】:

我创建了一个带有表单的 HTML 页面,它接受一个名称和密码并将其传递给一个 Python 脚本,该脚本应该打印带有欢迎消息的人名。但是,在我发布值之后,我只是在浏览器中显示 Python 代码,而不是欢迎消息。我已经将 html 文件和 python 文件存储在 Apache 2.2 下的 cgi-bin 文件夹中。如果我只是在浏览器中运行一个简单的 hello world python 脚本,则会显示“Hello World”消息。我正在使用 WinXP、Python 2.5、Apache 2.2。我试图运行的代码如下:

#!c:\python25\python.exe
import cgi
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
reshtml = """Content-Type: text/html\n
<html>
 <head><title>Security Precaution</title></head>
 <body>
 """

print reshtml 

User = form['UserName'].value
Pass = form['PassWord'].value

if User == 'Gold' and Pass == 'finger':
    print '<big><big>Welcome'
    print 'mr. Goldfinger !</big></big><br>'
    print '<br>'
else:
    print 'Sorry, incorrect user name or password' 
    print '</body>'
print '</html>'

它的答案可能很明显,但它完全逃避了我。我对 Python 很陌生,因此我们将不胜感激。

谢谢。

【问题讨论】:

  • 你为什么不使用 mod_wsgi 或合适的 web 框架?

标签: python


【解决方案1】:

您可能必须像这样提取字段值

User = form.getfirst('UserName')
Pass = form.getfirst('PassWord')

我知道,这很奇怪。

【讨论】:

    【解决方案2】:

    这个

    我只是得到 Python 代码 显示在浏览器中

    听起来 Apache 和 Python 的 CGI 处理配置不正确。

    您可以通过将 UserName 和 PassWord 作为 GET 参数传递来缩小测试用例:

    http://example.com/cgi-bin/my-script.py?UserName=Foo&PassWord=bar
    

    如果你这样做会发生什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      • 2016-06-11
      • 2020-10-10
      相关资源
      最近更新 更多