生活随笔
收集整理的這篇文章主要介紹了
Python之编写登录接口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
編寫登陸接口
輸入用戶名密碼
認證成功后顯示歡迎信息
輸錯三次后鎖定
本文采用的文本在代碼中已詳細注釋,流程圖如下:
代碼如下:
#!/usr/bin/env python
# Author:Zhu dongdong
import os
import sys
#the article use file :
'''
username_Password.txt:
aaa 111
bbb 222
ccc 333
ddd 444
lock_username.txt:
aaa
ddd
'''
user_name = ""
password = ""
count = 0#the count is used for cycle(while)
count1 = 0#the count1 is used for judging
#check username is lock or not
def check_username_isLock():with open(r"E:/lock_username.txt")as file2:locklist = file2.readlines()for lock_name in locklist:username = lock_name.strip("\n")if username == user_name :print("the operation is too frequently")exit()return
#return 1 represent that username is exist and return 0 against
def check_username_isExist():x = 0with open(r"E:/userName_Password.txt")as file1:list = file1.readlines()for List_user_andName in list:(user, password1) = List_user_andName.strip("\n").split(" ")if user_name == user:x = 1return x
#check the password is right
def check_password_isok(count1):with open(r'E:/username_Password.txt')as file:list = file.readlines()for user in list:(username,passsword1) = user.strip("\n").split(" ")if user_name == username and password == passsword1:print("Welcome to login...")exit()elif user_name == username and password != passsword1:count1 = count1 + 1print("The input of password is error!")return count1
#write the username to 'lockfile'
def write_lockusername():file = open("E:lock_username.txt", "a")file.write('''\n{a}'''.format(a = user_name))return
while count == 0:user_name = input("please input the username:")check_username_isLock()ret = check_username_isExist()if ret == 1:#represent username is existpassword = input("Please input the password:")breakelse :print("The username is error,Please try again.")
while count == 0:count1 = check_password_isok(count1)if count1 == 3:write_lockusername()exit(0)password = input("Please input the password:")
總結
以上是生活随笔為你收集整理的Python之编写登录接口的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。