使用python编写一个登录接口

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用python编写一个登录接口相关的知识,希望对你有一定的参考价值。

需求:

编写登录接口

输入用户名密码

认证成功后 显示欢迎信息

输入三次后锁定


用户信息文件

黑名单的文件

     黑名单里检查,不让登录

     用户名密码判定


流程图:

技术分享


代码:

#!/usr/bin/env python
tries = 0
lockfile = open(‘account_lock.txt‘,‘r+‘,encoding=‘UTF-8‘)
tolockfile = open(‘account_lock.txt‘,‘a‘,encoding=‘UTF-8‘)
userfile = open(‘account.txt‘,‘r‘,encoding=‘utf-8‘)

def islock(account):
    for line in lockfile:
        line = line.strip(‘\n‘)
        if line == account:
            print(‘此账号已锁定‘)
            exit()

def inaccount(account):
    for line in userfile:
        col1_user,col2_pass = line.strip().split()
        if col1_user == account:
            passtries = 0
            while passtries < 3:
                pass1 = input(‘请输入密码:‘)
                if col2_pass == pass1:
                   print(‘欢迎使用‘)
                   exit()
                else:
                   passtries +=1
            else:
                tolockfile.write(‘%s\n‘ %(account))
                print(‘账号已锁定!‘)
                exit()

        else:
            print(‘此账号不存在‘)
            break

while tries<3:
    account = input(‘请输入账号:‘)
    islock(account)
    inaccount(account)
    tries +=1
else:
    print(‘此用户真的不存在 88‘)
    exit()

lockfile.close()
userfile.close()
tolockfile.close()



# with open(‘account_lock.txt‘,‘a‘,encoding=‘UTF-8‘) as data:  
#     data.write("aaa")  
#此方法可以方式忘记文件close
#tolockfile = open(‘account_lock.txt‘,‘a‘,encoding=‘UTF-8‘)
#open文件后进行追加至最后一行




本文出自 “Open World” 博客,请务必保留此出处http://voidyao000.blog.51cto.com/12458042/1903626

以上是关于使用python编写一个登录接口的主要内容,如果未能解决你的问题,请参考以下文章

使用python编写一个登录接口

python编写登录接口

8.Python编写登录接口

用Python编写登录接口

Python之编写登录接口

Python 编写用户登录接口