我有一个登录 python 脚本,我想在其中获取用户名和密码并将其传递给另一个 python 脚本,该脚本使用数据库中的值验证用户名和密码。如果用户存在,它将创建一个cookie,并在cookie中设置用户名值并重定向到下一页,否则,它将返回到上一页。
这是我的 index.py 代码:
#!/usr/bin/python
import cgi;
import cgitb;
import sqlite3;
import os;
import Cookie;
import sys;
cgitb.enable()
form= cgi.FieldStorage()
username= None
userID = None
userPW= None
#Open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()
def createdb():
###Create table login
conn.execute(""" CREATE TABLE login (userid INTEGER PRIMARY KEY ,
username TEXT, passwrd TEXT)""")
##
###Create table excursion
conn.execute(""" CREATE TABLE excursion (excurid INTEGER PRIMARY KEY,
location TEXT, excurDate TEXT, excurTime TEXT, user INTEGER, FOREIGN KEY(user) REFERENCES login(userid))""")
##
#Create table sighting
conn.execute(""" CREATE TABLE sighting (sightid INTEGER PRIMARY KEY,
species TEXT, observation TEXT, loginuser INTEGER, userexcursion INTEGER, FOREIGN KEY(loginuser, userexcursion) REFERENCES excursion (user, excurid))""")
##
#Insert username and password in login table
conn.execute("""INSERT INTO login (userid,username,passwrd) VALUES(NULL,'Diego','diego')""")
conn.commit()
#Insert dummy data in excursion table
conn.execute("""INSERT INTO excursion (excurid,location,excurDate,excurTime,user) VALUES(NULL,'Macquarie','04/01/2012','6:00pm',1)""")
conn.execute("""INSERT INTO excursion (excurid,location,excurDate,excurTime,user) VALUES(NULL,'Carlton','04/05/2012','7:00am',1)""")
conn.commit()
#Insert dummy data in sighting table
conn.execute("""INSERT INTO sighting (sightid,species,observation,loginuser,userexcursion) VALUES(NULL,'Duck','long beak',1,1)""")
conn.execute("""INSERT INTO sighting (sightid,species,observation,loginuser,userexcursion) VALUES(NULL,'Parrots','beautiful and colorful',1,2)""")
conn.commit()
conn.close()
if not os.path.exists("manager.db"):
createdb();
#define start of page
pagehead1= """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Home </title>
<link rel='stylesheet' type='text/css' href='/index.css/'/>
</head>
<body>
"""
pagehead2="""
<form method=POST action="http://localhost:8000/cgi-bin/validate.py">
<div id="container">
<div id="header">
<h1> Field Note Manager </h1>
<p class= "description"> Observe...Record...Save! </p>
</div>
<div id="wrapper">
<div id="content">
<p> Username: <input type="text" name="usernameLogin"/> </p>
<br/>
<p> Password: <input type="password" name="passwordLogin"/> </p>
<br/>
<p id="login"> <input type="submit" name="loginBtn" value="Login"/> </p>
</div>
</div>
<div id="footer">
<p> Copyright 42578647, 2012 </p>
</div>
</div>
"""
pagefoot= """ </form>
</body>
</html> """
print "Content_type: text/html\n\n"
print pagehead1
print pagehead2
print pagefoot
这是我使用数据库验证用户名的代码:
#! usr/local/bin/python
import cgi;
import cgitb;
import Cookie;
import os;
import sqlite3;
#open connection
conn= sqlite3.connect("manager.db")
cur= conn.cursor()
username= None
form= cgi.FieldStorage()
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>
"""
def validate(UserPW):
sql= "SELECT * FROM login"
userPWDatabase=cur.execute(sql)
cur.fetchall()
for record in userPWDatabase:
if 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
return 1
else:
return 0
我不知道问题出在哪里