NameError:名称'pass_result'未定义第25行?

时间:2015-06-18 15:55:11

标签: python html cgi nameerror

的index.html:

<html>
<head>
<title>Login Page</title>
<link type="text/css" rel="stylesheet" href="coach.css" />
</head>
<body>
<img src="images/logo-cel-transparent_0.png" width="74" height="64"><strong><img src="images/logo-cel-transparent_0.png" alt="Cel logo" width="74" height="64" align="right">
</strong>
<h1 align="center"><strong>Central Electronics Limited</strong></h1>
<p>&nbsp;</p>
<h2 align="center">Storage Management System</h2>
<p>&nbsp;</p>
<p align="center">Login To System</p>
<p align="center">&nbsp;</p>
<form action="cgi-bin/validate.py" method="post">
  <div align="center">Username :
    <input type="text" name="username">
    <br>
    Password :
    <input type="text" name="password">
    <br>
    <input type="submit" value="Submit">
  </div>
</form>
<p align="center">&nbsp;</p>
</body>
</html>

validate.py:

import cgi
import yate
import sqlite3
import sys


connection = sqlite3.connect('users.sqlite')
cursor = connection.cursor()


print('Content-type:text/html')
form=cgi.FieldStorage()
for each_form_item in form.keys():
  if (each_form_item=='username'):
    username=form[each_form_item].value
  if (each_form_item=='password'):
    password=form[each_form_item].value

result=cursor.execute('SELECT USERNAME from validate')
usernames=[row[0] for row in result.fetchall()]
print(usernames)
for each_username in usernames:
    if (username==each_username):
        pass_result=cursor.execute('SELECT PASSWORD from validate where username=?',(each_username,))
    password1=[row[0] for row in pass_result.fetchall()]
    for each_password in password1:
        if (each_password==password):
            with open("C:\Python34\ProjectShivam\webapp\cgi-bin\successvalidate.py") as f:
              code = compile(f.read(), "successvalidate.py", 'exec')
              exec(code) 

        else:

            print('')
            print('Login Failure')

successvalidate.py:

import yate


print(yate.start_response())

print(yate.para("Login Successful"))
print(yate.include_footer({"Click here to Go to Welcome Page":"/welcome.html"}))

simple_httpd.py(服务器代码):

from http.server import HTTPServer, CGIHTTPRequestHandler

port = 8080

httpd = HTTPServer(('', port), CGIHTTPRequestHandler)
print("Starting simple_httpd on port: " + str(httpd.server_port))
httpd.serve_forever()

我使用命令提示符运行服务器(simple_httpd.py)。索引页面打开。我输入第一组用户名和密码。它按预期运行,并且successvalidate.py打开。但是,当我输入第二组用户名和密码(即users.sqlite中的第二行表验证)(验证表包含两组用户名和密码)时,它显示在cmd上:

127.0.0.1 - - [18/Jun/2015 20:59:29] b'Traceback (most recent call last):\r\n  F
ile "C:\\Python34\\ProjectShivam\\webapp\\cgi-bin\\validate.py", line 25, in <mo
dule>\r\n    password1=[row[0] for row in pass_result.fetchall()]\r\nNameError:
name \'pass_result\' is not defined\r\n'

此外,任何其他用户名都不会导致在Web浏览器上打印“登录失败”文本,而是在服务器上显示相同的错误。有什么问题?

1 个答案:

答案 0 :(得分:0)

如果不满足此条件,您将收到错误:

if (username==each_username):
    pass_result=cursor.execute('SELECT PASSWORD from validate where username=?',(each_username,))

将默认值设置为pass_result,例如pass_result = None然后在使用之前处理它,例如if pass_result is not None: