python基础篇 09-文件读写、list练习
pwd.txt文件格式为:
练习1:
import string """ 需求: 1、注册的程序,账号和密码存在文件里面 1、最多输入3次 2、输入账号和密码、确认密码,密码长度要在6-12位之间 2.1(密码包含大写字母、小写字母、数字,选做) 3、账号/密码/确认密码输入为空要提示 4、用户已经存在不能注册 5、两次密码要输入一致 """ def username_pwd_valid(usrinfo): if len(list(filter(lambda c:c in string.digits,usrinfo))) and len(list(filter(lambda c:c in string.ascii_lowercase,usrinfo))) and len(list(filter(lambda c:c in string.ascii_uppercase,usrinfo))): return True return False def get_username(): with open('pwd.txt', encoding='utf-8') as f: content = f.readlines() usr_names = list(map(lambda usr_pwd: usr_pwd.strip().split(',')[0], content)) return usr_names USR_NAMES = get_username() for i in range(3): username = input("请输入用户名:").strip() passwd = input("请输入密码:").strip() confirmpwd = input("请输入确认密码:").strip() if not (username and passwd and confirmpwd): print("用户名/密码/确认密码任意一个都不能为空") elif passwd != confirmpwd: print("密码和确认密码不一致!") elif not ((len(username) >=6 and len(username) <= 12) and (len(passwd) >= 6 and len(passwd) <= 12)): print("用户名、密码、确认密码长度要再6-12之间") elif not (username_pwd_valid(username) and username_pwd_valid(passwd)): print("用户名、密码、确认密码必须包含大小写字母和数字!") else: if username in USR_NAMES: print(f"此{username}用户已经存在!") else: with open('pwd.txt', 'a+', encoding='utf-8') as f: write_content = ','.join([username, passwd + '\n']) f.writelines(write_content) # 或者 # write_content = username+','+passwd+'\n' # f.write() print(f"添加用户{username},{passwd}成功!") break else: print("注册次数超限!")
练习2:
第一种解决方案:
import string
"""
需求:
2、登录,账号密码从文件里面取
1、最多输入3次
2、账号/密码的为空校验
3、账号不存在要提示
4、登录成功结束
"""
with open('pwd.txt', encoding='utf-8') as fr:
content = fr.readlines()
usr_name_list = list(map(lambda user_info: user_info.strip().split(',')[0], content))
pwd_list = list(map(lambda user_info: user_info.strip().split(',')[1], content))
for i in range(3):
username = input("请输入用户名:").strip()
password = input("请输入密码:").strip()
if not(username and password):
print("用户名和密码都不能为空!")
else:
if username not in usr_name_list:
print(f"用户名{username}不存在!")
else:
pwd = pwd_list[usr_name_list.index(username)]
if pwd == password:
print(f"用户{username}登陆成功!")
break
else:
print("用户名密码错误,登陆失败哦!")
else:
print("登陆次数超限")
第二种解决方案:
# 另一种解决方案:
USER_INFO = {} with open(r'b.txt',encoding='utf-8') as fr: line = fr.readline() if line: uname = line.strip().split(',')[0] upwd = line.strip().split(',')[1] USER_INFO[uname] = upwd for i in range(3): username = input("请输入用户名:").strip() password = input("请输入密码:").strip() if not (username and password): print("用户名和密码都不能为空!") else: if username not in USER_INFO: print(f"用户名{username}不存在!") elif USER_INFO.get(username) != password: print("用户名密码错误,登陆失败哦!") else: print(f"用户{username}登陆成功!") break else: print("登陆次数超限")
练习3:
1、监控日志文件,找到每分钟请求大于200的ip地址,加入黑名单
日志文件格式为:
import collections import time file_pointer = 0 # ip_dict = collections.defaultdict(int) while True: # ips = {} ips = collections.defaultdict(int) # f = open('access.log',encoding='utf-8') # f.seek(file_pointer) # for line in f: # ip = line.strip().split()[0] # if ip in ips: # ips[ip] += 1 # else: # ips[ip] = 1 # file_pointer = f.tell() # f.close() # for ip in ips: # if ips[ip] >= 200: # print(f"要加入黑名單的ip地址是:{ip}") # time.sleep(5) with open('access.log',encoding='utf-8') as f: f.seek(file_pointer) for line in f: ip = line.strip().split()[0] ips[ip] += 1 # if ip in ips: # ips[ip] += 1 # else: # ips[ip] = 1 file_pointer = f.tell() for ip in ips: if ips[ip] >= 200: print(f"要加入黑名單的ip地址是:{ip}") time.sleep(5)