How to make script like this?
$python script.py
1. Register
2. Login
3. View User
Choose with number :
I can make a script for Register and Login, but i still confusing make a script for view user.
Spoiler For View User:
Choose with number : 3 (If still not login yet)
"Please Login First!"
Choose witn number : 3 (After Login)
[{'username' : xxxx , 'password' : xxx}]
Here the script:
import hashlib ,os
resource_file = "passwords.txt"
def encode(username,password):
return "$%s::%s$"%(username,hashlib.sha1(password).hexdigest())
def add_user(username,password):
if os.path.exists(resource_file):
with open(resource_file) as f:
if "$%s::"%username in f.read():
raise Exception("user already exists")
with open(resource_file,"w") as f:
print >> f, encode(username,password)
return username
def check_login(username,password):
with open(resource_file) as f:
if encode(username,password) in f.read():
return username
def create_username():
try:
username = add_user(raw_input("enter username:"),raw_input("enter password:"))
print "Added User! %s"%username
except Exception as e:
print "Failed to add user %s! ... user already exists??"%username
def login():
if check_login(raw_input("enter username:"),raw_input("enter password:")):
print "Login Success!!"
else:
print "there was a problem logging in"
def view_user:
#Still Dont know
menu = """
1. Login
2. Register
3. View User """
print menu
while True:
{'2':create_username,'l':login, '3' view_user}.get(raw_input("Chosee with number (1,2,3: ").lower(),login)()
Note: xxxx is just sample for username and password has been created by register...
I'm using Python 2.7
OS windows 7