day11作业

帅兰,干就完了四五二合一

# 4、
# 4.1:编写用户登录接口
# 4.2:编写程序实现用户注册后(注册到文件中),可以登录(登录信息来自于文件)
# 5、下属两个案例的升级需求完成
# 做合法性校验:
# 1、如果输入的用户名包含特殊字符^$&...让用户重新输入
# 2、如果输入的用户名已经存在也重新输入
# 1、密码长度不能小于八
# 升级需求1:同一个账号输错三次则退出
# 升级需求2:同一个账号输错三次则,该账号则锁定10秒,即便程序被终止,仍然计时


# 以上要求都全实现
import time

user_info = {}
list1 = {'^', '$', '&'}

while True:

    print('''登入or注册:
    1、登入
    2、注册
    3、退出
    ''')

    h = input('请选择:').strip()

    if h == '1':

        user = input('请输入用户名:').strip()
        password = input('请输入密码:').strip()

        with open('a.txt', mode='rt', encoding='utf-8')as f:

            for i in f:
                user3, pwd, count, count1 = i.strip().split(':')
                user_info.setdefault(user3, [pwd, int(count), float(count1)])

                if user in user_info:
                    now_time = time.time()

                    if now_time <= user_info[user][2]:
                        time3 = user_info[user][2]
                        print(f'当前用户被锁定了!剩余时间为{time3 - now_time}秒')
                        break

                if user == user3 and pwd == password:
                    print('登入成功,指令q退出')

                    while True:
                        xc = input('请输入指令:').strip()

                        if xc == 'q':
                            print('已退出')
                            break

                        print('请输入正确指令')
                        continue
                    break

            else:
                print('账号密码错误')

                if user in user_info:
                    user_info[user][1] += 1

                    if user_info[user][1] >= 3:
                        lock_time = time.time() + 100
                        user_info[user][2] += lock_time
                        user_info[user][1] -= 3
                continue

    elif h == '2':

        user1 = input('请输入您的用户名:').strip()
        pwd1 = input('请输入您的密码:').strip()

        for u in user1:

            if u in list1:
                print('账号含有特殊字符')
                continue

        for y in pwd1:

            if y in list1:
                print('密码含有特殊字符')
                continue

        if len(pwd1) < 8:
            print('密码长度小于八位')
            continue

        else:
            with open('a.txt', mode='a+t', encoding='utf-8')as f1, open('a.txt', mode='rt', encoding='utf-8')as f2:

                for i in f2:
                    user3, pwd, count, count1 = i.strip().split(':')
                    user_info.setdefault(user3, [pwd, int(count), float(count1)])

                    if user1 in user_info:
                        print('账户已存在')
                        break

                else:
                    f1.write(f'{user1}:{pwd1}:{0}:{0}\n')
                    print('注册成功')
                    continue

    elif h == '3':

        with open('a.txt', mode='wt', encoding='utf-8')as kl:
            pass

        for jk in user_info:
            n1 = jk
            pwd3 = user_info[jk][0]
            count2 = user_info[jk][1]
            t = user_info[jk][2]

            with open('a.txt', mode='at', encoding='utf-8')as hj:
                hj.write(f'{n1}:{pwd3}:{count2}:{t}\n')
        print('已退出')
        break

    else:
        print('请输入正确类型')

# 1、通用文件copy工具实现

a = input('请输入源文件路径:')
b = input('请输入新文件路径:')
with open(f'{a}', mode='rb', ) as f, open(f'{b}', mode='wb')as s:
    for i in f:
        s.write(i)

# 2、基于seek控制指针移动,测试r+、w+、a+模式下的读写内容
# 文件内容:帅的太自然

# r+t  0
with open('a.txt', mode='r+', encoding='utf-8') as f:
    f.seek(3, 0)
    print(f.tell())
    print(f.read())
    f.write('帅的太自然\n')  # 写入文件末尾
    #  3
    #  的太自然

# a+b 1
with open('a.txt', mode='a+b') as f:
    f.seek(-5, 1)
    print(f.tell())
    print(f.read().decode('utf-8'))
    # 12
    # 然

# w+b 2
with open('a.txt', mode='w+b') as f:
    f.write('帅的太自然'.encode('utf-8'))
    f.seek(-12, 2)
    print(f.tell())
    print(f.read().decode('utf-8'))
    # 3
    # 的太自然

# 3、tail -f access.log程序实现

import time

while True:
    with open('access.log',mode='at',encoding='utf-8')as f:
        times = time.strftime("%Y-%m-%d %H:%M:%S")
        msg = f'位于{times}帅兰又变帅了\n'
        f.write(msg)
        time.sleep(5)

import time

with open('access.log', mode='rb') as f:
    f.seek(0, 2)
    while True:
        line = f.readline()
        if len(line) == 0:
            time.sleep(1)
        else:
            print(line.decode('utf-8'), end='')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值