HTML表单不能使用python

2024-05-04 20:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个带有表单的HTML页面,它接受一个名称和密码,并将其传递给Python脚本,该脚本将打印带有欢迎消息的人名。但是,在我发布这些值之后,我只得到在浏览器中显示的Python代码,而不是欢迎消息。我已经将html文件和python文件存储在apache2.2下的cgi-bin文件夹中。如果我只是在浏览器中运行一个简单的helloworld python脚本,就会显示“hello world”消息。我使用的是WinXP、python2.5、apache2.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非常陌生,所以如果有任何帮助,我将不胜感激。在

谢谢。在


Tags: 文件代码importform脚本消息titlehtml
2条回答

您可能需要像这样提取字段值

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

我知道,这很奇怪。在

这个

i'm just getting the Python code displayed in the browser

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

通过将用户名和密码作为GET参数传递,可以缩小测试用例的范围:

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

如果你这么做会怎么样?在

相关问题 更多 >