最近在学习python,其中有个作业要求用python写个用户登录接口,大体完成功能有:
1、用户如果输入正确的用户名和密码,在显示登录成功
2、用户如果输入错误的用户名则显示无效用户名
3、如果密码错误,让其重试三次,三次以后则进行锁定
本脚本是我通过shell脚本实现的相应功能,仅供后续通过python实现参考。
#!/bin/bash
userfile=/root/aa
lockfile=/root/lk
read -p "Please input your username:" user
read -p "Please input your password:" pass
if [ -z ${user} ] || [ -z ${pass} ];then
echo "Username or Password is not allowed null"
exit
fi
chk_user=`cat ${userfile}|awk '{print $1}'|egrep -iw ${user} |wc -l`
chk_lk=`cat ${lockfile}|egrep -iw ${user} |wc -l`
if [ ${chk_lk} -ne 0 ] && [ ${chk_user} -eq 1 ] ;then
echo "${user} locked "
exit
fi
while read line
#for line in `cat ${userfile}`
do
username=`echo $line |egrep -iw "${user}"|awk '{print $1}'`
password=`echo $line |egrep -iw "${user}"|awk '{print $2}'`
if [ ! -z ${username} ] && [ ${username} == ${user} ];then
break
elif [ ! -z ${user} ] && [ -z ${username} ] && [ ${chk_user} -eq 1 ] ;then
continue
else
echo "Invalid username ${user}"
exit
fi
done < ${userfile}
if [ ${username} == ${user} ] && [ ${password} == ${pass} ];then
echo "${username} login success"
elif [ ${username} == ${user} ] && [ ${password} != ${pass} ];then
echo "Invalid password"
count=0
while [[ ${count} -lt 2 ]]
do
read -p "Please input your password:" pass
if [ ${password} == ${pass} ];then
echo "${username} login success"
break
else
count=$((${count}+1))
fi
if [ ${count} -eq 2 ];then
echo ${user} >> ${lockfile}
echo "User is locked now,please contact the administrator !"
fi
done
else
echo "Invalid User ${username}"
fi