· 8 years ago · Apr 18, 2018, 12:56 AM
1import sqlite3
2import re
3
4# Database Connection
5
6conn = sqlite3.connect("accounts.db")
7
8c = conn.cursor()
9
10# Create table
11
12c.execute("""CREATE TABLE IF NOT EXISTS accounts (
13 username text,
14 password text
15 )""")
16
17def signup(username, password):
18 u = 0
19 p = 0
20 u_req = "You must have a username with a minimum length of 8 and a maximum length of 16!"
21 p_req = "Your password must have at least one letter, one number, and one character from \"$#@\"!"
22 if len(username) >= 8 and len(username) <= 16:
23 c.execute("SELECT * FROM accounts WHERE username=?",
24 (username)
25 )
26 if c.fetchall() != []:
27 print("Sorry, but the username \"" + username + "\" is taken!")
28 else:
29 u = 1
30 m1 = re.search('[a-zA-Z]', password)
31 hasLet = m1[0].isalpha()
32 hasNum = any(char.isdigit() for char in password)
33 if password.includes("$") or password.includes("#") or password.includes("@"):
34 hasSpecChar = True
35 if hasLet == True and hasNum == True and hasSpecChar == True:
36 p = 1
37 if u == 1 and p == 1:
38 c.execute("INSERT INTO accounts (username, password) VALUES (?, ?)",
39 (username, password)
40 )
41 conn.commit()
42 elif u == 0 and p == 0:
43 print(u_req + "\n" + p_req)
44 elif u == 0:
45 print(u_req)
46 elif p == 0:
47 print(p_req)
48
49def login(username, password):
50 c.execute("SELECT * FROM accounts WHERE username=? AND password=?",
51 (username, password)
52 )
53
54 if c.fetchall() != []:
55 return username + password
56 else:
57 print("Invalid username or password.")
58 main()
59
60# Main Program
61
62def main():
63 # Prompts
64 choice1 = input("Would you like to signin or signup? ")
65 if choice1.lower() == "signin" or choice1.lower() == "login":
66 login(input("Username: "), input("Password: "))
67 elif choice1.lower() == "signup" or choice1.lower() == "register":
68 signup(input("Username: "), input("Password: "))
69
70if __name__ == "__main__":
71 main()