Python实现简单登录验证


Posted in Python onApril 13, 2016

本文实例为大家分享了简单的Python登录验证,供大家参考,具体内容如下

编写登录接口

要求:1、输入用户名密码

 2、认证成功后显示欢迎信息

 3、输错三次后锁定

#coding=utf-8
__author__ = 'wangwc'

import sys,os
count = 0
locked = 0
mark_user = 0
mark_passwd = 0
#获取路径
def cur_file_dir():
  path = sys.path[0]
  if os.path.isdir(path):
    return path
  elif os.path.isfile(path):
    return os.path.dirname(path)
#print (cur_file_dir())
path = cur_file_dir()
#print(path)
path1 = path.replace("\\",'/') + '/'
#print (path1)
#path2 = path1 + '/'

#循环输入
while count < 3:
  name = input("Username:").strip()
  if len(name) == 0:
    print ("Username can not be empty....")
    continue
  key = input("Password:").strip()
  if len(key) == 0:
    print("The password can not be empty!Try again...")
    continue
  f = open(path1 + "username.txt","r")
  userlist = f.readlines()
  for user in userlist:
    if user.strip() == name:
      mark_user = 1
  f.close()

  if mark_user == 1:
    f = open(path1 + "%s_lock.txt" %(name),"r")
    locked = int(f.readline().strip())
    f.close()
  else:
    print ("Username or Passsord wrong....")
    break
  if locked == 1:
    print("Sorry, the username had been locked!!!Please call the system administrator...")
  else:
    f = open (path1 + "%s_passwd.txt" %(name),"r")
    passwd = (f.readline().strip())
    if passwd.strip() == key:
      mark_passwd = 1
    if mark_user == 1 and mark_passwd == 1:
      f = open("%s_count.txt" %(name),"w")
      f.write("0")
      f.close()
      print("%s,welcome BABY!" %(name) )
      #input('Press Enter to exit')
    else:
      f = open("%s_count.txt" %(name),"r")
      count = int((f.read().strip()))
      f.close()
      count +=1
      f = open("%s_count.txt" %(name),"w")
      f.write(str(count))
      f.close()
      print ("Username or password wrong!And the username '%s' has %d more chances to retry!" %(name,3 - count))
      if(count == 3):
        print ("'%s' has been locked!!!" %(name))
        if os.path.exists(path1 + "%s_lock.txt" %(name)):
          fobj = open(path1 + "%s_lock.txt" %(name),"w")
          fobj.writelines("1\n")
        else:
          print ("Username or password wrong!")
      continue

以上就是本文的全部内容,希望对大家的学习有所帮助。

Python 相关文章推荐
Python 文件读写操作实例详解
Mar 12 Python
跟老齐学Python之玩转字符串(1)
Sep 14 Python
python爬虫基本知识
Mar 05 Python
Python之csv文件从MySQL数据库导入导出的方法
Jun 21 Python
pytorch训练imagenet分类的方法
Jul 27 Python
python删除字符串中指定字符的方法
Aug 13 Python
Django 对IP访问频率进行限制的例子
Aug 30 Python
django 将自带的数据库sqlite3改成mysql实例
Jul 09 Python
浅析Python 多行匹配模式
Jul 24 Python
celery在python爬虫中定时操作实例讲解
Nov 27 Python
利用python如何实现猫捉老鼠小游戏
Dec 04 Python
python操作xlsx格式文件并读取
Jun 02 Python
Python数组定义方法
Apr 13 #Python
Python基于select实现的socket服务器
Apr 13 #Python
Python简单实现子网掩码转换的方法
Apr 13 #Python
Python多进程分块读取超大文件的方法
Apr 13 #Python
Python字符串拼接、截取及替换方法总结分析
Apr 13 #Python
Python字符串格式化输出方法分析
Apr 13 #Python
PHP网页抓取之抓取百度贴吧邮箱数据代码分享
Apr 13 #Python
You might like
Zend引擎的发展 [15]
2006/10/09 PHP
php提交过来的数据生成为txt文件
2016/04/28 PHP
映彩衣的js随笔(js图片切换效果)
2011/07/31 Javascript
javascript中attribute和property的区别详解
2014/06/05 Javascript
jQuery中position()方法用法实例
2015/01/16 Javascript
jQuery插件PageSlide实现左右侧栏导航菜单
2015/04/12 Javascript
JS中获取函数调用链所有参数的方法
2015/05/07 Javascript
详解JavaScript逻辑Not运算符
2015/12/04 Javascript
JS与jQ读取xml文件的方法
2015/12/08 Javascript
Angular.Js的自动化测试详解
2016/12/09 Javascript
js select下拉联动 更具级联性!
2020/04/17 Javascript
关于在vue 中使用百度ueEditor编辑器的方法实例代码
2018/09/14 Javascript
监听angularJs列表数据是否渲染完毕的方法示例
2018/11/07 Javascript
vue滚动固定顶部及修改样式的实例代码
2019/05/30 Javascript
Webpack中SplitChunksPlugin 配置参数详解
2020/03/24 Javascript
vant-ui AddressEdit地址编辑和van-area的用法说明
2020/11/03 Javascript
[02:28]DOTA2英雄基础教程 灰烬之灵
2013/12/19 DOTA
[01:29]2014DOTA2展望TI 剑指西雅图DK战队专访
2014/06/30 DOTA
[52:37]完美世界DOTA2联赛循环赛 Forest vs DM BO2第一场 10.29
2020/10/29 DOTA
Python 3 实现定义跨模块的全局变量和使用教程
2019/07/07 Python
ASOS比利时:英国线上零售商及自有品牌
2018/07/29 全球购物
网络通讯中,端口有什么含义,端口的取值范围
2012/11/23 面试题
运动会开幕式解说词
2014/02/05 职场文书
函授自我鉴定范文
2014/02/06 职场文书
企业年会主持词
2014/03/27 职场文书
优秀护士先进事迹
2014/05/08 职场文书
2014年车间工作总结
2014/11/21 职场文书
合作合同协议书范本
2015/01/27 职场文书
考察邀请函范文
2015/01/31 职场文书
社区工作者个人总结
2015/02/28 职场文书
《平移和旋转》教学反思
2016/02/19 职场文书
表扬信范文
2019/04/22 职场文书
2019年自助餐厅创业计划书模板
2019/08/22 职场文书
导游词之沈阳植物园
2019/11/30 职场文书
一劳永逸彻底解决pip install慢的办法
2021/05/24 Python
新手必备Python开发环境搭建教程
2021/05/28 Python