【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

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

相关文章
|
20天前
|
机器学习/深度学习 存储 算法
解锁文件共享软件背后基于 Python 的二叉搜索树算法密码
文件共享软件在数字化时代扮演着连接全球用户、促进知识与数据交流的重要角色。二叉搜索树作为一种高效的数据结构,通过有序存储和快速检索文件,极大提升了文件共享平台的性能。它依据文件名或时间戳等关键属性排序,支持高效插入、删除和查找操作,显著优化用户体验。本文还展示了用Python实现的简单二叉搜索树代码,帮助理解其工作原理,并展望了该算法在分布式计算和机器学习领域的未来应用前景。
|
4月前
|
C语言 Python
python 调用c接口
【10月更文挑战第12天】 ctypes是Python的一个外部库,提供和C语言兼容的数据类型,可以很方便地调用C DLL中的函数
80 0
|
1月前
|
API Python
【02】优雅草央央逆向技术篇之逆向接口协议篇-以小红书为例-python逆向小红书将用户名转换获得为uid-优雅草央千澈
【02】优雅草央央逆向技术篇之逆向接口协议篇-以小红书为例-python逆向小红书将用户名转换获得为uid-优雅草央千澈
104 1
|
2月前
|
数据采集 存储 XML
Python爬虫:深入探索1688关键词接口获取之道
在数字化经济中,数据尤其在电商领域的价值日益凸显。1688作为中国领先的B2B平台,其关键词接口对商家至关重要。本文介绍如何通过Python爬虫技术,合法合规地获取1688关键词接口,助力商家洞察市场趋势,优化营销策略。
|
19天前
|
API 文件存储 数据安全/隐私保护
python 群晖nas接口(一)
这段代码展示了如何通过群晖NAS的API获取认证信息(SID)并列出指定文件夹下的所有文件。首先,`get_sid()`函数通过用户名和密码登录NAS,获取会话ID(SID)。接着,`list_file(filePath, sid)`函数使用该SID访问FileStation API,列出给定路径`filePath`下的所有文件。注意需替换`yourip`、`username`和`password`为实际值。
72 18
|
17天前
|
API Python
python泛微e9接口开发
通过POST请求向指定IP的API注册设备以获取`secrit`和`spk`。请求需包含`appid`、`loginid`、`pwd`等头信息。响应中包含状态码、消息及`secrit`(注意拼写)、`secret`和`spk`字段。示例代码使用`curl`命令发送请求,成功后返回相关信息。
38 5
|
30天前
|
监控 网络安全 开发者
Python中的Paramiko与FTP文件夹及文件检测技巧
通过使用 Paramiko 和 FTP 库,开发者可以方便地检测远程服务器上的文件和文件夹是否存在。Paramiko 提供了通过 SSH 协议进行远程文件管理的能力,而 `ftplib` 则提供了通过 FTP 协议进行文件传输和管理的功能。通过理解和应用这些工具,您可以更加高效地管理和监控远程服务器上的文件系统。
58 20
|
17天前
|
API 文件存储 Python
python 群晖nas接口(二)
这段代码展示了如何通过API将文件上传到群晖NAS。它使用`requests`库发送POST请求,指定文件路径、创建父级目录及覆盖同名文件的参数,并打印上传结果。确保替换`yourip`和`sid`为实际值。
43 2
|
1月前
|
存储 数据采集 数据处理
如何在Python中高效地读写大型文件?
大家好,我是V哥。上一篇介绍了Python文件读写操作,今天聊聊如何高效处理大型文件。主要方法包括:逐行读取、分块读取、内存映射(mmap)、pandas分块处理CSV、numpy处理二进制文件、itertools迭代处理及linecache逐行读取。这些方法能有效节省内存,提升效率。关注威哥爱编程,学习更多Python技巧。
|
1月前
|
存储 JSON 对象存储
如何使用 Python 进行文件读写操作?
大家好,我是V哥。本文介绍Python中文件读写操作的方法,包括文件读取、写入、追加、二进制模式、JSON、CSV和Pandas模块的使用,以及对象序列化与反序列化。通过这些方法,你可以根据不同的文件类型和需求,灵活选择合适的方式进行操作。希望对正在学习Python的小伙伴们有所帮助。欢迎关注威哥爱编程,全栈路上我们并肩前行。

热门文章

最新文章