【Python之旅】第一篇:基于文件处理的登陆接口

简介:

1.基本需求

    编写登陆接口,实现如下需求:

(1)输入用户名密码

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

(3)输错三次后锁定




2.实现细节


·每添加一个用户,需要手动添加三个文件

文件 功能
username_count.txt 记录用户输错密码的次数,最大为3次,如果用户密码输入正确,则重置为0,默认为0
username_lock.txt 记录用户是否被锁定,1表示锁定,0表示未锁定,默认为0
username_passwd.txt 记录用户的密码

·注:username是指该用户的用户名,视具体的用户名而定;


·每添加一个用户,在username.txt中写入该用户名,一个用户名占一行(手动);

·如果输入的用户名或密码为空,都要求重新输入;

·输入不存在的用户名,只会提示“username or password wrong”;

·输入存在的用户名,才会进行密码输入错误次数的记录。




3.实现代码与注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import  sys,os
 
count =  0   #To count,  if  the user  is  locked.
mark_user =  0    #To make sure  if  the user  is  existing.
mark_passwd =  0  #To make sure  if  the password  is  right.
 
while  count <  3 :
   name = raw_input( 'Username:' ).strip()
   if  len(name) ==  0 :
     print  'The username can not be empty!'
     continue
   
   key = raw_input( 'Password:' ).strip() 
   if  len(key) ==  0 :
     print  'The password can not be empty!Try again!'
     continue
 
#Upon:To check  if  the username or password  is  empty.检查用户名或密码是否为空
   
   f = file( 'username.txt' 'r' )
   userlist = f.readlines()
   f.close()
   for  user  in  userlist:
     if  user.strip() == name:
       mark_user =  1
 
#检查输入的用户是否在username.txt文件中,即是否注册,如果在,mark_user =  1
   
   if  mark_user ==  1 :  
     f = file( '%s_lock.txt'  % (name),  'r' )
     locked =  int (f.readline().strip())
     f.close()  
   else :
     print  'Username or password wrong!'  
     break
#Upon:To check  if  exist, if  not exist,printing username or password wrong!And quit the program.
#用户名不存在就提示用户名或密码错误,用户名存在就检查该用户是否被锁定
 
   if  locked ==  1 :
     
     sys.exit( 'Sorry, the username had been locked!!!Please call the system administrator.' )
 
#Upon:To check  if  the username  is  locked by the system.
 
   else :
     f = file( '%s_passwd.txt'  % (name),  'r' )
     passwd = (f.readline().strip())
     if  passwd.strip() == key:
         mark_passwd =  1
     # check the password from the name_input password file.
 
     if  mark_user ==  1  and mark_passwd ==  1 :
       f = file( '%s_count.txt'  % name,  'w' )
       f.write( '0' )
       f.close()
       sys.exit( '%s, welcome to our system!'  % name)
 
#用户名和密码正确,将username_count.txt内容重置为 0 
      
     else :
       f = file( '%s_count.txt'  % name,  'r' )
       count =  int ((f.read().strip()))
       f.close()
       count +=  1
       f = file( '%s_count.txt'  % name,  'w' )
       f.write(str(count))
       f.close()
 
#open the count file of the user and change it.
#密码不正确,就把count次数记录在该用户名对应的文件count文件上
       
       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( '%s_lock.txt'  % (name)):
           fobj = open( '%s_lock.txt'  % (name),  'w' )
           fobj.writelines( '1\n' )
         else :
           print  'Username or password wrong!'
       continue
 
#Upon:To check  if  the username and password are right,  if  not, the count will add  1 .
#如果count次数为 3 ,则将用户对应的lock文件内容重置为 1 ,锁定该用户




4.测试


--对单一用户xpleaf的测试


·在对应4个文件中添加相应信息:

1
2
3
4
5
6
7
8
9
10
11
xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt xpleaf_count.txt xpleaf_lock.txt xpleaf_passwd.txt 
==> username.txt <==
xpleaf
 
==> xpleaf_count.txt <==
0
==> xpleaf_lock.txt <==
0
 
==> xpleaf_passwd.txt <==
xpleaf123


·正常输入用户名和密码:

1
2
3
4
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!


·故意输错两次密码后查看相关文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:xpleaf
Password:klkdf
Username or password wrong!
And the username  'xpleaf'  has  2  more chances to retry!
Username:xpleaf
Password:kldf
Username or password wrong!
And the username  'xpleaf'  has  1  more chances to retry!
Username:Traceback (most recent call last):
   File  "newlogin.py" , line  8 in  <module>
     name = raw_input( 'Username:' ).strip()
EOFError
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt 
2

·可以看到count文件已经记录为2,再输入正确的用户名和密码:

1
2
3
4
5
6
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_count.txt 
0

·count文件重置为0;


·连续输入密码错误超过3次:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:xpleaf
Password:kldf
Username or password wrong!
And the username  'xpleaf'  has  2  more chances to retry!
Username:xpleaf
Password:klkdf
Username or password wrong!
And the username  'xpleaf'  has  1  more chances to retry!
Username:xpleaf
Password:kldf
Username or password wrong!
And the username  'xpleaf'  has  0  more chances to retry!
'xpleaf'  has been locked!!!
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:xpleaf
Password:xpleaf123
Sorry, the username had been locked!!!Please call the system administrator.
xpleaf@xpleaf-machine:~/seminar6/day1$ cat xpleaf_lock.txt 
1

·用户已经被锁定,对应lock文件内容变为1,如果需要解锁,则要手动将该文件重置为0;


·输入错误的用户名:

1
2
3
4
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:klkdlf
Password:klkdf
Username or password wrong!

·只会提示用户名或密码错误,不会做相关文件记录;


--对两个用户xpleaf和yonghaoyip的测试


·为用户yonghaoyip添加相关文件信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
xpleaf@xpleaf-machine:~/seminar6/day1$ head username.txt yonghaoyip_count.txt yonghaoyip_lock.txt yonghaoyip_passwd.txt 
==> username.txt <==
xpleaf
yonghaoyip
 
==> yonghaoyip_count.txt <==
0
 
==> yonghaoyip_lock.txt <==
0
 
==> yonghaoyip_passwd.txt <==
yonghaoyip123


·主要测试两个用户的count文件记录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
对用户yonghaoyip操作:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:yonghaoyip
Password:klkdf
Username or password wrong!
And the username  'yonghaoyip'  has  2  more chances to retry!
Username:yonghaoyip
Password:kldf
Username or password wrong!
And the username  'yonghaoyip'  has  1  more chances to retry!
Username:Traceback (most recent call last):
   File  "newlogin.py" , line  8 in  <module>
     name = raw_input( 'Username:' ).strip()
EOFError
 
对用户xpleaf操作:
xpleaf@xpleaf-machine:~/seminar6/day1$ python newlogin.py 
Username:xpleaf
Password:kkjlkdf
Username or password wrong!
And the username  'xpleaf'  has  2  more chances to retry!
Username:xpleaf
Password:xpleaf123
xpleaf, welcome to our system!
 
查看两个用户对应的count文件记录:
xpleaf@xpleaf-machine:~/seminar6/day1$ head yonghaoyip_count.txt xpleaf_count.txt 
==> yonghaoyip_count.txt <==
2
==> xpleaf_count.txt <==
0

·可以看到两个用户的相关文件并不会相互影响。

相关文章
|
4天前
|
数据挖掘 Python
🚀告别繁琐!Python I/O管理实战,文件读写效率飙升的秘密
在日常编程中,高效的文件I/O管理对提升程序性能至关重要。Python通过内置的`open`函数及丰富的库简化了文件读写操作。本文从基本的文件读写入手,介绍了使用`with`语句自动管理文件、批量读写以减少I/O次数、调整缓冲区大小、选择合适编码格式以及利用第三方库(如pandas和numpy)等技巧,帮助你显著提升文件处理效率,让编程工作更加高效便捷。
16 0
|
2天前
|
Java Python
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
【9月更文挑战第18天】在 Python 中,虽无明确的 `interface` 关键字,但可通过约定实现类似功能。接口主要规定了需实现的方法,不提供具体实现。抽象基类(ABC)则通过 `@abstractmethod` 装饰器定义抽象方法,子类必须实现这些方法。使用抽象基类可使继承结构更清晰、规范,并确保子类遵循指定的方法实现。然而,其使用应根据实际需求决定,避免过度设计导致代码复杂。
|
4天前
|
Python
全网最适合入门的面向对象编程教程:Python函数方法与接口-函数与方法的区别和lamda匿名函数
【9月更文挑战第15天】在 Python 中,函数与方法有所区别:函数是独立的代码块,可通过函数名直接调用,不依赖特定类或对象;方法则是与类或对象关联的函数,通常在类内部定义并通过对象调用。Lambda 函数是一种简洁的匿名函数定义方式,常用于简单的操作或作为其他函数的参数。根据需求,可选择使用函数、方法或 lambda 函数来实现代码逻辑。
|
23天前
|
安全 项目管理 Python
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
使用Python shutil库进行文件和目录操作
|
11天前
|
Java 数据安全/隐私保护 Python
Python案例分享:如何实现文件的解压缩
Python案例分享:如何实现文件的解压缩
38 8
|
11天前
|
存储 缓存 安全
Python案例分享:如何实现文件的上传下载
Python案例分享:如何实现文件的上传下载
51 6
|
27天前
|
数据挖掘 数据处理 数据格式
Python读取.nc文件的方法与技术详解
通过上述方法,用户可以根据需求选择合适的库来读取.nc文件,并根据实际情况进行必要的数据操作,这是科学数据处理和分析中的一个重要技能。
50 10
|
23天前
|
Python
像导入Python模块一样导入ipynb文件
像导入Python模块一样导入ipynb文件
|
24天前
|
Ubuntu Linux 数据安全/隐私保护
使用Cython库包对python的py文件(源码)进行加密,把python的.py文件生成.so文件并调用
本文介绍了在Linux系统(Ubuntu 18.04)下将Python源代码(`.py文件`)加密为`.so文件`的方法。首先安装必要的工具如`python3-dev`、`gcc`和`Cython`。然后通过`setup.py`脚本使用Cython将`.py文件`转化为`.so文件`,从而实现源代码的加密保护。文中详细描述了从编写源代码到生成及调用`.so文件`的具体步骤。此方法相较于转化为`.pyc文件`提供了更高的安全性。
33 2
|
26天前
|
网络协议 测试技术 网络安全
Python进行Socket接口测试的实现
在现代软件开发中,网络通信是不可或缺的一部分。无论是传输数据、获取信息还是实现实时通讯,都离不开可靠的网络连接和有效的数据交换机制。而在网络编程的基础中,Socket(套接字)技术扮演了重要角色。 Socket 允许计算机上的程序通过网络进行通信,它是网络通信的基础。Python 提供了强大且易于使用的 socket 模块,使开发者能够轻松地创建客户端和服务器应用,实现数据传输和交互。 本文将深入探讨如何利用 Python 编程语言来进行 Socket 接口测试。我们将从基础概念开始介绍,逐步引导大家掌握创建、测试和优化 socket 接口的关键技能。希望本文可以给大家的工作带来一些帮助~