python 作业
1.增删改查haproxy.conf配置文件
1.查询输入:www.oldboy1.com
2.删除输入:{'backend': 'www.oldboy2.org','record':{'server': ["1.1.1.1","2.2.2.2"],'weight': 20,'maxconn': 30}}
3.增加输入:{'backend': 'www.oldboy2.org','record':{'server': ["1.1.1.1","2.2.2.2"],'weight': 20,'maxconn': 30}}
4.修改输入:{'backend': 'www.oldboy2.org','record':{'server': ["1.1.1.1","2.2.2.2"],'weight': 20,'maxconn': 30}}
修改之后:{'backend': 'www.oldboy2.org','record':{'server': ["1.1.1.1","2.2.2.2"],'weight': 20,'maxconn': 30}}
1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 7 defaults 8 log global 9 mode http 10 timeout connect 5000ms 11 timeout client 50000ms 12 timeout server 50000ms 13 option dontlognull 14 15 listen stats :8888 16 stats enable 17 stats uri /admin 18 stats auth admin:1234 19 20 frontend oldboy.org 21 bind 0.0.0.0:80 22 option httplog 23 option httpclose 24 option forwardfor 25 log global 26 acl www hdr_reg(host) -i www.oldboy.org 27 use_backend www.oldboy.org if www 28 29 backend www.oldboy1.org 30 server 100.1.7.9 weight 20 maxconn 1111111 31 server 100.1.7.9 weight 20 maxconn 0 32 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33 33 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000 34 server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000 35 36 backend www.oldboy2.org 37 server 1.1.1.1 2.2.2.2 weight 20 maxconn 30 38 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33 39 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000 40 41 backend www.oldboy20.org 42 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33 复制代码
1 import os 2 3 4 def menu(): 5 menu = ( 6 """ 7 1. 增加 8 2. 删除 9 3. 修改 10 4. 查找""") 11 print(menu.lstrip(' ')) 12 13 def InsertConf(): 14 content = eval(input("please input content:")) 15 server1 = content["record"]["server"][0] 16 server2 = content["record"]["server"][1] 17 weight = content["record"]["weight"] 18 maxconn = content["record"]["maxconn"] 19 with open("hafile",mode="r",encoding="utf8") as f,open("hafile.bak",mode="w+",encoding="utf8") as f_bak: 20 for line in f: 21 if line.startswith("backend") and content["backend"] in line: 22 f_bak.write(line) 23 f_bak.write(" server %s %s weight %d maxconn %d " % (server1,server2, weight, maxconn)) 24 continue 25 f_bak.write(line) 26 os.rename("hafile","hafile.obj") 27 os.rename("hafile.bak","hafile") 28 29 def DeleteConf(): 30 flag = False 31 choice = eval(input("input content")) 32 with open("hafile", encoding="utf8") as f_read, open("hafile.bak", mode="w", encoding="utf8") as f_write: 33 for line in f_read: 34 if line.startswith("backend") and choice["backend"] in line: 35 flag = True 36 if choice["record"]["server"][0] in line 37 and choice["record"]["server"][1] in line 38 and str(choice["record"]["weight"]) in line 39 and str(choice["record"]["maxconn"]) in line 40 and flag == True: 41 flag = False 42 continue 43 f_write.write(line) 44 os.rename("hafile","hafile.obj") 45 os.rename("hafile.bak","hafile") 46 47 def UpdateConf(): 48 flag = False 49 Original = eval(input("input Original content")) 50 Modified = eval(input("input Modified content")) 51 Modified = " server %s %s weight %s maxconn %s " % (Modified["record"]["server"][0], 52 Modified["record"]["server"][1], 53 Modified["record"]["weight"], 54 Modified["record"]["maxconn"]) 55 with open("hafile", encoding="utf8") as f_read, open("hafile.bak", mode="w", encoding="utf8") as f_write: 56 for line in f_read: 57 if line.startswith("backend") and Original["backend"] in line: 58 flag = True 59 print(line) 60 if Original["record"]["server"][0] in line 61 and Original["record"]["server"][1] in line 62 and str(Original["record"]["weight"]) in line 63 and str(Original["record"]["maxconn"]) in line 64 and flag == True: 65 flag = False 66 f_write.write(Modified) 67 continue 68 f_write.write(line) 69 def FindConf(): 70 recode = [] 71 flag = False 72 ipname = input("please input domain name:") 73 with open("hafile",mode="r",encoding="utf8") as f: 74 for line in f: 75 if line.startswith("backend") and ipname in line: 76 flag = True 77 continue 78 if line.startswith("backend")and flag == True: 79 break 80 if flag: 81 recode.append(line.strip()) 82 for value in recode: 83 print(" %s"%(value.rstrip())) 84 85 def Main(): 86 menu() 87 choice = int(input("input number:")) 88 return choice 89 90 if __name__ == "__main__": 91 while True: 92 obj = Main() 93 if obj == 1: 94 InsertConf() 95 elif obj ==2: 96 DeleteConf() 97 elif obj == 3: 98 UpdateConf() 99 elif obj == 4: 100 FindConf() 101 else: 102 continue
2.用户认证
用户输入账号密码三次错误,程序终止
如果三次都是同一用户错误,锁定用户
1
|
1. 定义一个字典,用户每次错误向字典增加记录{name count},当用户输入错误 3 次退出时, 判断字典的值是否大于 3 ,大于 3 写到锁定文件 |
1 alex alex3417 2 oldboy oldboy110 3 oldgirl oldgirl110
1 accounts = {} 2 3 def lock_user(name): 4 with open("lock_user", mode="r+", encoding="utf8") as f_lock: 5 for line in f_lock: 6 if line.strip().split()[0] == name: 7 print("Lock user") 8 exit() 9 10 def lockd_user(**kwargs): 11 with open("lock_user",mode="a+",encoding="utf8") as f_lockd_user: 12 for key in kwargs: 13 if kwargs[key] >2: 14 f_lockd_user.write(key + " ") 15 16 17 18 19 def check_user(name,passwd): 20 with open("user",mode="r",encoding="utf8") as f_check: 21 for line in f_check: 22 if name == line.strip().split()[0]: 23 if passwd == line.strip().split()[1]: 24 print("Success") 25 exit() 26 else: 27 add_error(name) 28 return name 29 return name 30 31 def add_error(name): 32 if accounts: 33 if name in accounts: 34 accounts[name] += 1 35 else: 36 accounts[name] = 1 37 else: 38 accounts[name] = 1 39 40 def main(): 41 count = 0 42 while True: 43 name = input("input name: ") 44 passwd = input("input passwd: ") 45 lock_user(name) #判断用户是否锁定 46 name = check_user(name,passwd) #判断用户 47 count += 1 48 if count > 2: 49 lockd_user(**accounts) 50 print("exit than three") 51 break 52 53 54 if __name__ == '__main__': 55 main() 复制代码