Skip to content Skip to sidebar Skip to footer

Redirect Python Script To Another Python Script For Validation Of Login Credentials

I have a login python script in which i want to take the username and password and pass it to another python script which validates the username and password with the values in the

Solution 1:

Could you please give more details than "I don't know where is the problem": what exactly happens, what log messages you get if there are some.

Have you tried to move the "validate" function definition before it's called? I think this can be part of your problem.


Update: after some investigations and few fixes, I got it working:

  1. As said previously, the "validate" function definition should be set before it's called
  2. Your sql query is not correct, as "SELECT * FROM login" will return 3 fields ( id, username and passwrd), so you should change it to "SELECT username,passwrd FROM login"
  3. The record returned by sqlite is a tuple, and you're trying to compate it to a list, so there's a TYPE mismatch. One solution if to change to : "if list(record) == UserPW:"

Also, under ubuntu looking at /var/log/apache2/error.log helps a lot.

Which finally led to:

#!/usr/bin/env pythonimport cgi;
import cgitb;
import Cookie;
import os;
import sqlite3;

#open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()

username= None
form= cgi.FieldStorage()


defvalidate(UserPW):
      sql= "SELECT username,passwrd FROM login;"
      cur.execute(sql)
      userPWDatabase=cur.fetchall()
      for record in userPWDatabase:
          iflist(record) == UserPW:
              #Create cookie
              C= Cookie.SimpleCookie()
              #take the value of the index.py form variable username
              username= form.getvalue('usernameLogin')
              #set the cookie with the usernameLogin key
              C['usernameLogin']= username
              print C
              return1else:
              return0              



UserPW= [form.getvalue('usernameLogin'), form.getvalue('passwordLogin')]
isValidate = validate(UserPW);

if isValidate == 1:
      print"Content-type: text/html\n\n"print"""
      <html>
          <head> Redirecting </head>
          <body>
              <form method= POST action="http://localhost:8000/cgi-bin/page1.py">
              <p> Validated! <input type="submit" value="Enter"/> </p>
              </form>
          </body>
      </html> """elif isValidate == 0:
      print"Content-type: text/html\n\n"print"""
      <html>
          <head> Redirecting </head>
          <body>
              <form method=POST action= "http://localhost:8000/cgi-bin/index.py">
                  <p> Username or Password incorrect! <input type="submit" value="Go back"/> </p>
              </form>
          </body>
      </html>
      """

Solution 2:

I changed the code validate.py to this:

#! usr/local/bin/pythonimport cgi;
import cgitb;
import Cookie;
import os;
import sqlite3;

cgitb.enable()
username= None
form= cgi.FieldStorage()

#open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()

pagehead= """
    <html>
        <head> Redirecting </head>
        <body>

            """
pagefoot="""<form method= POST action="http://localhost:8000/cgi-bin/page1.py">
            <p> Validated! <input type="submit" value="Enter"/> </p>
            </form>
        </body>
    </html> """
errorpagefoot= """
<form action="http://localhost:8000/cgi-bin/index.py">
<p> Error! <input type="submit" value="Go Back"/> </p>
</form>
</body>
</html>"""print"Content_type: text/html\n\n"print pagehead
userName= form.getvalue('usernameLogin')
userPW= form.getvalue('passwordLogin')
userPWDatabase = conn.execute("SELECT username,passwrd FROM login WHERE username=? and passwrd=?",[userName,userPW])
cur.fetchone()
for result in userPWDatabase:
    userDb= result[0]
    pwDb= result[1]
    if userDb == userName and pwDb == userPW:
        #Create Cookie
        C= Cookie.SimpleCookie()
        #take the value of usernameLogin into the variable username
        username= form.getvalue('usernameLogin')
        #Set-Cookie header with the usernameLogin key
        C['usernameLogin'] = username
        print C
        print pagefoot    
    elif userDb != userName and pwDb != userPW:
        print errorpagefoot

it's working now but it doesnt redirect to the previous page anymore, incase of incorrect username or password

Solution 3:

Ok, after reading your new code it seems you still have several bugs, but the main error I think is the way you try to check the login/password: first you select it from the database byt filtering on the username and password, which can only return 1 or 0 result as the login is a primary in your db, and then you try to loop on the results and check if the username and password are both correct, or both incorrect, which consequently does not cover the case when username is correct but not the password ( and inverse ). So I suggest you change the last part of your code for this:

print"Content-type: text/html\n\n"print pagehead
userName= form.getvalue('usernameLogin')
userPW= form.getvalue('passwordLogin')
cur.execute("SELECT username,passwrd FROM login WHERE username=?",[userName])
userPWDatabase = cur.fetchone()
if userPWDatabase isnotNone:
        userDb= userPWDatabase[0]
        pwDb= userPWDatabase[1]
        #Create Cookie
        C= Cookie.SimpleCookie()
        #take the value of usernameLogin into the variable username
        username= form.getvalue('usernameLogin')
        #Set-Cookie header with the usernameLogin key
        C['usernameLogin'] = username
        print C
        print pagefoot    
else:
        print errorpagefoot

Note that you could also keep your SQL query with filter on username and password, but then you don't need to check anything else: if there's no result, one of the two field is incorrect, and there's one it's necessarily good.

In addition, I don't know what's your aim ( either to learn cgi-bin/python or setup a real application ), but you might be interested to have a look at Django ( www.djangoproject.com )

Solution 4:

i just read that the Set-Cookie header stuff is suppose to be before the line:

print "Content_type: text/html\n\n"

so, i fixed my code again and it's working fine now. it goes back to previous script in case of wrong username / password and it proceeds to next script after validation Here is the final code:

#! usr/local/bin/pythonimport cgi;
import cgitb;
import Cookie;
import os;
import sqlite3;

cgitb.enable()
username= None
form= cgi.FieldStorage()

#open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()

pagehead= """
    <html>
        <head> Redirecting </head>
        <body>

            """
pagefoot="""<form method= POST action="http://localhost:8000/cgi-bin/page1.py">
            <p> Validated! <input type="submit" value="Enter"/> </p>
            </form>
        </body>
    </html> """
errorpagefoot= """
<form action="http://localhost:8000/cgi-bin/index.py">
<p> Error! <input type="submit" value="Go Back"/> </p>
</form>
</body>
</html>"""


userName= form.getvalue('usernameLogin')
userPW= form.getvalue('passwordLogin')
userPWDatabase = conn.execute("SELECT username,passwrd FROM login WHERE username=? and passwrd=?",[userName,userPW])
cur.fetchone()
for result in userPWDatabase:
    userDb= result[0]
    pwDb= result[1]
    if userDb == userName and pwDb == userPW:
        #Create Cookie
        C= Cookie.SimpleCookie()
        #take the value of usernameLogin into the variable username
        username= form.getvalue('usernameLogin')
        #Set-Cookie header with the usernameLogin key
        C['usernameLogin'] = username
        print C
    elif userDb != userName and pwDb != userPW:
        print errorpagefoot

print"Content_type: text/html\n\n"print pagehead
if username:
    print pagefoot
else:
    print errorpagefoot

@Alexis

Thanks for your time and help Alexis :)

Post a Comment for "Redirect Python Script To Another Python Script For Validation Of Login Credentials"