需求:
1、查 输入:www.oldboy.org 获取当前backend下的所有记录 2、新建 输入: {‘backend‘: ‘www.oldboy‘,‘record‘: {‘server‘: ‘100.1.7.9‘,‘weight‘: 20,‘maxconn‘: 30}} arg = { ‘backend‘: ‘www.oldboy.org‘, ‘record‘:{ ‘server‘: ‘100.1.7.9‘, ‘weight‘: 20, ‘maxconn‘: 30 } } 3、删除 输入: arg = { ‘bakend‘: ‘www.oldboy.org‘, ‘record‘:{ ‘server‘: ‘100.1.7.9‘, ‘weight‘: 20, ‘maxconn‘: 30 } }
文件:(configuration)
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 infodefaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
code:
1 # Author:P J J 2 import os 3 4 #查询 5 def select(domain): 6 result_list = [] #存放查询的结果 7 flag = False # 定义flag,目的是为了判断是否查到了backend 8 9 with open("configuration",‘r‘,encoding=‘utf-8‘) as f: 10 for line in f: 11 if line.strip() == "backend %s" % domain: 12 flag = True #表示找到了 13 continue 14 if line.strip().startswith(‘backend‘): 15 flag = False 16 if flag: 17 result_list.append(line.strip()) #line.strip()这步很关键,不然删除的时候根本就不匹配! 18 print(result_list) 19 return result_list #一定要返回,不然删除的时候 fetch_list为空 20 21 #新增 22 def add(infor): 23 backend_title = arg.get("backend") # 要插入的backend的名称 24 context_title = "backend %s" % (backend_title) # 要插入backend整个字段 25 record_title = arg["record"] 26 context_record = "server %s %s weight %s maxconn %s" % (record_title["server"], 27 record_title["server"], record_title["weight"],record_title["maxconn"]) 28 29 find_flag = False 30 # 查找是否存输入已存在 31 with open(‘configuration‘, ‘r‘, encoding=‘utf-8‘) as f: 32 for line in f: 33 if line.strip() == "backend %s" % backend_title: 34 find_flag = True 35 continue 36 if line.strip().startswith("backend"): 37 flag = False 38 39 # 新建的时候判断是否已经存在 40 if find_flag: 41 print(‘已经存在!‘) 42 else: 43 with open(‘configuration‘, ‘r‘, encoding=‘utf-8‘) as f, open(‘configuration_new‘, ‘w+‘, 44 encoding=‘utf-8‘) as f_new: 45 for line in f: 46 f_new.write(line) 47 f_new.write(‘\n‘ + context_title + "\n") 48 new_backend = " " * 8 + context_record + "\n" 49 f_new.write(new_backend) 50 print("新建成功!") 51 os.rename(‘configuration‘, ‘configuration.bak‘) 52 os.rename(‘configuration_new‘, ‘configuration‘) 53 os.remove(‘configuration.bak‘) 54 55 #删除 56 def delete(dict_info): 57 # 1.首先获取用户的记录并以列表的形式存储,以便于我们调用 58 del_backend = dict_info["backend"] 59 del_record = dict_info["record"] 60 context_title = "backend %s" % (del_backend) 61 context_record = "server %s %s weight %s maxconn %s" % ( 62 del_record["server"], del_record["server"], del_record["weight"], del_record["maxconn"]) 63 64 # 调用上面定义的fetch()函数,得到包含backend内容的列表 65 fetch_list = select(del_backend) 66 # 判断1:若列表为空:直接返回 67 if not fetch_list: 68 return 69 # 判断2:否则不为空 70 else: 71 # 判断1:若用户输入的server信息不在fetch_list里面,直接返回 72 if context_record not in fetch_list: 73 print("你输入的信息不存在!") 74 return 75 # 判断2:若输入的server在fetch_list里面,就将此server信息从列表中移除 76 else: 77 fetch_list.remove(context_record) 78 with open(‘configuration‘, ‘r‘) as read_obj, open(‘configuration.new‘, ‘w‘) as write_obj: 79 flag = False 80 has_write = False 81 for line in read_obj: 82 if line.strip() == context_title: 83 flag = True 84 continue 85 if flag and line.startswith(‘backend‘): 86 flag = False 87 if flag: 88 if not has_write: 89 print(fetch_list) 90 for new_line in fetch_list: 91 temp = "%s%s\n" % (" " * 8, new_line) 92 write_obj.write(temp) 93 has_write = True 94 else: 95 write_obj.write(line) 96 os.rename(‘configuration‘, ‘configuration.bak‘) 97 os.rename(‘configuration.new‘, ‘configuration‘) 98 os.remove(‘configuration.bak‘) 99 print("已经删除!") 100 101 102 103 104 #保证别人想用时,不会执行后面的东西 105 if __name__ == ‘__main__‘: 106 #得到用户的输入信息 107 chooses = ‘‘‘ 108 1 》》》》》查找 109 2 》》》》》新建 110 3 》》》》》删除 111 ‘‘‘ 112 print(chooses) 113 while True: 114 user_choose = input(‘请输入:‘) 115 #根据用户的输入调用不同函数 116 if user_choose.isdigit(): 117 user_choose = int(user_choose) 118 if user_choose == 1: 119 domain = input("请输入要查找的域名:") 120 select(domain) 121 elif user_choose == 2: 122 print("arg = {‘backend‘: ‘www.oldboy‘,‘record‘: {‘server‘: ‘100.1.7.9‘,‘weight‘: 20,‘maxconn‘: 30}}") 123 arg = eval(input("新建arg:")) 124 add(arg) 125 elif user_choose == 3: 126 print("arg = {‘backend‘: ‘www.oldboy‘,‘record‘: {‘server‘: ‘100.1.7.9‘,‘weight‘: 20,‘maxconn‘: 30}}") 127 arg = eval(input("删除arg:")) 128 delete(arg) 129 else: 130 print(‘输入的值不存在!‘) 131 elif user_choose ==‘q‘: 132 exit("程序已经退出!") 133 else: 134 print("输入错误!")
感想:
编写这个配置文件有很多难点。查询是最容易实现的,接着是新建,新建中有一个点就是要判断是否已经存在,最后我个人认为最难的一点就是删除,删除要用到查询的方法,然后把查到的内容存到一个列表里面,因为在python中,列表的增删改查实现起来很容易。在删除会遇到很多坑。。我就不多说了,出现问题的话自己打断点调试。以后编写大概都是这个形式。从后面开始,就开始面向对象编程了,函数也会经常用了!自己建了一个学习群:625386800,适合新手学习Python,后面我会把我学习的资料上传到群文件,有兴趣的可以一起加群交流
时间: 2024-11-03 21:54:11