标签:res host get connect try gis ini ret print
1 import hashlib, redis 2 3 4 class loginRegistration: 5 def __init__(self): 6 # decode_responses=True连接redis存的数据是字符串格式 7 pool = redis.ConnectionPool(host=‘127.0.0.1‘, password=‘‘, port=6379, decode_responses=True) 8 # 连接池 9 self.r = redis.Redis(connection_pool=pool) 10 11 # 注册 12 def registration(self): 13 # 判断username是否存在 14 user = self.r.hlen("username") 15 # 如果存在 16 if user == 0: 17 print(‘注册用户为0‘) 18 # 否则 19 else: 20 print(‘注册人数为‘, user, "个") 21 userInputName = input(‘请输入用户名‘) 22 userInputPassword = input(‘请输入密码‘) 23 # 用来判断用户是否存在 24 a = self.r.hmget("username", userInputName) 25 if a != [None]: 26 print("用户已被注册") 27 return False 28 # 将秘密已MD5输入到数据库 29 passwordMd5 = hashlib.md5() 30 passwordMd5.update(userInputPassword.encode(‘utf8‘)) 31 password = passwordMd5.hexdigest() 32 # 添加数据,用try来检测提交是否成功 33 try: 34 self.r.hmset("username", {userInputName: password}) 35 print("注册成功") 36 except: 37 print("注册失败") 38 39 # 登录 40 def login(self): 41 userInputName = input(‘请输入用户名‘) 42 userInputPassword = input(‘请输入密码‘) 43 passwordMd5 = hashlib.md5() 44 passwordMd5.update(userInputPassword.encode(‘utf8‘)) 45 password = passwordMd5.hexdigest() 46 a = self.r.hkeys("username") 47 if userInputName not in a: 48 print("用户不已存在") 49 return False 50 a = self.r.hget("username", userInputName) 51 print(a) 52 if a == password: 53 print("登录成功") 54 else: 55 print("登录失败") 56 57 58 lr = loginRegistration() 59 while True: 60 print("1、注册 2、登录") 61 userinput = input("") 62 if userinput is "1": 63 lr.registration() 64 elif userinput is "2": 65 lr.login()
标签:res host get connect try gis ini ret print
原文地址:https://www.cnblogs.com/liyaobin/p/10241017.html