功能
1.随机验证码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 只有字母 import random temp="" for i in range(4): rad= random.randrange(65, 91) c = chr(rad) temp=temp+c print(temp) # 有数字有字母 import random temp="" for i in range(6): num = random.randrange(0, 100) if num>0 and num<=9: temp = temp + str(num) else: rad = random.randrange(65, 91) c = chr(rad) temp=temp+c print(temp)
2.登录程序
# 用户登录程序 函数式编程 def login(username,password): """ 用于用户名密码的验证 :param username:用户名 :param password:密码 :return:True,用户名验证成功。False,用户验证失败 """ f = open("db","r",encoding="utf-8") for line in f: line=line.strip() # 默认strip无参数,去除空格和换行符 # 有参数,移除两端指定的值 line_list = line.split("$") if username==line_list[0] and password==line_list[1]: # print("登陆成功!") return True return False # 应用函数 # user = input("请输入用户名:") # pwd = input("请输入密码:") # is_login = login(user,pwd) # if is_login: # print("登陆成功") # else: # print("登录失败") def register(username,password): """ 注册 :param username: 用户名 :param password: 密码 :return:True 注册成功 """ with open("db", "a", encoding="utf-8") as f: temp="\n"+username+"$"+password #注意这的写法 f.write(temp) return True def user_exist(username): """ 查看用户名是否存在 :param username: 用户名 :return: 如果用户名已经存在返回True,不存在返回False 一行一行去查找,如果存在返True """ with open("db","r",encoding="utf-8") as f: for line in f: line=line.strip() line_list=line.split("$") if line_list[0]==username: return True return False def main(): print("欢迎登录xxx") inp=input("1:登录,2:注册") inp=int(inp) if inp==1: user = input("请输入用户名:") pwd = input("请输入密码:") is_login = login(user, pwd) if is_login: print("登陆成功") else: print("登录失败") elif inp==2: user = input("请输入用户名:") pwd = input("请输入密码:") is_exist=user_exist(user) if is_exist: print("用户名已经存在,无法注册") else: result = register(user,pwd) if result: print("注册成功") else: print("注册失败") main()
3.匹配ip地址
见视频186
4.加密登录
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/usr/bin/env python import hashlib def md5(arg): hash=hashlib.md5(bytes("sdagf2356",encoding="utf-8")) hash.update(bytes(arg,encoding="utf-8")) return hash.hexdigest() def login(username,password): f = open("db","r",encoding="utf-8") for line in f: line=line.strip() # 默认strip无参数,去除空格和换行符 # 有参数,移除两端指定的值 line_list = line.split("$") y=md5(password) if username==line_list[0] and str(y)==line_list[1]: # print("登陆成功!") return True return False def register(username,password): with open("db","a",encoding="utf-8") as p: x=md5(password) temp = "\n"+username+"$"+str(x) s=p.write(temp) return True def isexist(username): with open("db","r",encoding="utf-8") as s: for line in s: line.strip() line_list = line.split("$") if line_list[0] == username: return True return False def main(): print("欢迎登录xxx") inp=input("1:登录,2:注册") inp=int(inp) if inp==1: user = input("请输入用户名:") pwd = input("请输入密码:") is_login = login(user, pwd) if is_login: print("登陆成功") else: print("登录失败") elif inp==2: user = input("请输入用户名:") pwd = input("请输入密码:") is_exist=isexist(user) if is_exist: print("用户名已经存在,无法注册") else: result = register(user,pwd) if result: print("注册成功") else: print("注册失败") main()