Python 学习练习之用户登录接口

目的:

用python实现简单用户注册,登陆,加黑功能,放个流程图吧。以下内容仅为练习

162536_EBow_3023401.png

执行流程:

[root@zabbix login]# python user_login.py 
username:tom
password:123456
Are you want to register ? [y/n]y
Register successful,Please relogin !
[root@zabbix login]# cat user_regist_file.txt 
tom,123456
[root@zabbix login]# python user_login.py 
username:tom
password:123456
Welcome login in benmu management system!
[root@zabbix login]# python user_login.py 
username:tom
password:54321
Your username or password is not correct!
username:tom
password:54321
Your username or password is not correct!
username:tom
password:54321
Too many mistakes,your username is locked
[root@zabbix login]# python user_login.py 
username:tom
password:123456
Sorry,your username is locked! Please try later!

代码:

[root@zabbix login]# cat user_login.py 
#Author: Tom
import sys

def user_input():
    user_name = input("\033[31musername:\033[0m")
    user_passwd = input("\033[31mpassword:\033[0m")
    return user_name,user_passwd

def user_exit(user_name):
    with open('user_regist_file.txt','r') as f:
        for line in f:
            line = line.strip()
            line_list = line.split(',')
            if line_list[0] == user_name:
                return True
            else:
                return False

def user_regist(user_name,user_passwd):
    with open('user_regist_file.txt','a+') as f:
        tmp = user_name + ',' + user_passwd + '\n'
        f.write(tmp)

def user_login(user_name,user_passwd):
    with open('user_regist_file.txt','r') as f:
        for line in f:
            line = line.strip()
            line_list = line.split(',')
            if user_name == line_list[0] and user_passwd == line_list[1]:
                return True
            else:
                return False

def user_lock(user_name):
    with open('user_lock_file.txt','r') as f:
        for line in f:
            if user_name == line.strip():
                return True
            else:
                return False

def lock_user(user_name):
    with open('user_lock_file.txt','a+') as f:
        f.write(user_name + '\n')
        f.close()

def main():
    count = 0
    while count < 3:
        user_name,user_passwd = user_input()
        user_exit_result = user_exit(user_name)
        if user_exit_result:
            user_lock_result = user_lock(user_name)
            if user_lock_result:
                sys.exit("Sorry,your username is locked! Please try later!")
            else:
                user_login_result = user_login(user_name,user_passwd)
                if user_login_result:
                    sys.exit("Welcome login in benmu management system!")
                else:
                    if count < 2:
                        print("Your username or password is not correct!")
                        count +=1
                    else:
                        lock_user(user_name)
                        sys.exit('Too many mistakes,your username is locked')
        else:
            user_choice = input('Are you want to register ? [y/n]')
            if user_choice == 'y' or user_choice == 'Y':
                user_regist(user_name,user_passwd)
                sys.exit('Register successful,Please relogin !')
            elif user_choice == 'n' or user_choice == 'N':
                sys.exit('Please,do not leave me')
            else:
                sys.exit('Input Error,Bye Bye!')
    else:
        lock_user(user_name)

main()

为了方便大家交流,本人开通了微信公众号,和QQ群291519319。喜欢技术的一起来交流吧

转载于:https://my.oschina.net/u/3023401/blog/1514249

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值