NameError : name 'pass_result' not defined line 25?

347 views Asked by At

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(The server code):

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()

I run the server(simple_httpd.py) using command prompt. The index page opens up. I enter the 1st set of username and password. It runs as expected and successvalidate.py opens up. But when i enter the 2nd set of username and password(i.e, the second row of table validate in users.sqlite)(validate table contains two set of usernames and passwords), it displays on 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'

Also any other username does not result in the text 'Login Failure' being printed on the web browser but instead same error shows on server. What is wrong?

1

There are 1 answers

0
Dušan Maďar On BEST ANSWER

you are getting the error when this condition is not met:

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

set a default value to pass_result, e.g. pass_result = None and then handle it before using, e.g. if pass_result is not None: