标签:
设计一个验证用户登录的函数,根据用户输入的口令是否正确,返回True或False。数据库对用户明文密码进行了md5加密。
#!/usr/bin/env python
import hashlib
import sys
db = {
‘huangyi‘: ‘a35cc621314d2bdbf740a20670be2cbc‘,
‘rubi‘: ‘58e53d1324eef6265fdb97b08ed9aadf‘
}
def login(user, password):
if user not in db:
print ‘You have not Signed Up.‘
return False
else:
md5 = hashlib.md5()
md5.update(password)
if md5.hexdigest() == db[user]:
print ‘Old user‘
return True
else:
print ‘Wrong password‘
return False
if __name__==‘__main__‘:
if len(sys.argv) == 1 or len(sys.argv) == 2:
print ‘Useage:login user password‘
elif len(sys.argv) == 3:
login(sys.argv[1], sys.argv[2])
else:
print ‘Wrong‘
标签:
原文地址:http://my.oschina.net/lvyi/blog/383565