ha.txt文件内容:
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.baidu.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 server 100.1.7.8 100.1.7.8 weight 20 maxconn 4000 backend www.baidu1.com server 1.1.1.1 1.1.1.1 weight 20 maxconn 3000
操作代码ha.py:
import os #查询 def fetch(data): # 把整行作为一个整体,做成字符串拼接 # 相当于拼接backend www.baidu.com #record存放返回的数据 backend_data = "backend %s" %data record_list = [] #定义空列表 #打开文件 with open('ha.txt','r',encoding='utf8') as f: #在报警期间,一直执行捕捉操作 tag = False#报警器,默认不响 for line in f: #查找backend记录 #找backend,比较的时候把 去掉,用sttrip实现 if line.strip() == backend_data:#找到这行,在去除回车之后,是否等于backend tag = True#警报响起 continue#backend这行不要,跳出本次循环,下次循环都要带着tag警报了 # 解除警报 # 警报亮着,并且满足再次匹配backend的时候 #警报不能放到最后,在抓最后一个人之前,要把警报关闭 if tag and line.startswith("backend"): #tag = False # 解除警报,但是break之后什么变量都不生效了 break #当tag=True的情况 #警报亮了,并且有数据进来。line在文件中可能是空行,再加一个and操作 if tag and line: record_list.append(line.strip())#添加数据,每添加一行数据,就循环一次 #打印用户找到的信息 for line in record_list: print(line) return record_list#返回处理的信息 #添加 def add(data): backend = data['backend']#获取 record_list = fetch(backend)#传递给fetch #把用户输入的字典转换成server的模式 current_record = "server %s %s weight %s maxconn %s" %(data['record']['server'], data['record']['server'], data['record']['weight'], data['record']['maxconn']) backend_data = "backend %s" %backend#拼接字符串 #record_list为空的情况下,运行下面的代码 if not record_list: #加入record里面 record_list.append(backend_data)#标题 record_list.append(current_record)#server记录 #打开源文件,打开新文件把源文件内容一行行写入新文件 with open("ha.txt",'r',encoding="utf-8") as read_file, open("ha_new.txt",'w',encoding='utf-8') as write_file: #从源文件一行行读取内容 for r_line in read_file: write_file.write(r_line)#新文件一行行写 #用户输入的内容 for new_line in record_list:#读 if new_line.startswith('backend'):#是backend开头的文件 write_file.write(new_line + ' ')#往新文件里写 else:#其余行 write_file.write("%s%s " %(" "*8,new_line))#8个空格,写 else: #print(record_list) #自己创造一个record_list,把添加的server添加进去,保留原记录 record_list.insert(0,backend_data) if current_record not in record_list:# record_list.append(current_record)#添加自己拼接的记录 #print("new record list %s" %record_list)#新拼接的列表 with open('ha.txt','r',encoding='utf-8') as read_file, open('ha_new.txt','w',encoding='utf-8') as write_file: #警报 tag = False has_write = False for r_line in read_file:#源文件读 if r_line.strip() == backend_data:#拉响警报 tag = True continue #如果警报是响的并且是匹配到backend的情况 if r_line.strip() == backend_data: tag = True continue if not tag:#警报没有响 write_file.write(r_line)#一直往新文件里写 else:#警报响 if not has_write:#如果没有写的情况 for new_line in record_list: if new_line.startswith('backend'): write_file.write(new_line + ' ') else: write_file.write('%s%s ' %(' '*8,new_line)) has_write = True#record_list不会写 os.rename('ha.txt','ha_bak.txt') os.rename('ha_new.txt','ha.txt') os.remove('ha_bak.txt') #删除 def remove(data): backend = data['backend'] record_list = fetch(backend)#查 current_record = "server %s %s weight %s maxconn %s" % (data['record']['server'], data['record']['server'], data['record']['weight'], data['record']['maxconn']) backend_data = "backend %s" % backend #为空 if not record_list or current_record not in record_list: print(" 33[33;1m无此条记录 33[0m") return #终止掉 else: #处理record_list,存在,统一处理 record_list.insert(0,backend_data)#标题加进去 record_list.remove(current_record)#把用户删除的列表删掉 with open('ha.txt','r',encoding='utf-8') as read_file, open('ha_new.txt','w',encoding='utf-8') as write_file: tag = False has_write = False for r_line in read_file: if r_line.strip() == backend_data: tag = True continue if tag and r_line.startswith('backend'): tag = False if not tag: write_file.write(r_line) else: if not has_write: for new_line in record_list: if new_line.startswith('backend'): write_file.write(new_line + ' ') else: write_file.write('%s%s ' %(' '*8,new_line)) has_write = True os.rename('ha.txt','ha_bak.txt') os.rename('ha_new.txt','ha.txt') os.remove('ha_bak.txt') def change(data): #通过列表找到backend信息 backend = data[0]['backend'] record_list = fetch(backend)#找到backend信息 #改的数据,旧内容,新内容 old_record = "server %s %s weight %s maxconn %s" % (data[0]['record']['server'], data[0]['record']['server'], data[0]['record']['weight'], data[0]['record']['maxconn']) new_record = "server %s %s weight %s maxconn %s" % (data[1]['record']['server'], data[1]['record']['server'], data[1]['record']['weight'], data[1]['record']['maxconn']) backend_data = "backend %s" % backend #没有backend或者server信息不存在 if not record_list or old_record not in record_list: print(' 33[33;1m无此内容 33[0m') return else: record_list.insert(0,backend_data)#重新生成record_list index=record_list.index(old_record)#旧内容索引 record_list[index]=new_record#替换 with open('ha.txt', 'r', encoding='utf-8') as read_file, open('ha_new.txt', 'w', encoding='utf-8') as write_file: tag = False has_write = False for r_line in read_file: if r_line.strip() == backend_data: tag = True continue if tag and r_line.startswith('backend'): tag = False if not tag: write_file.write(r_line) else: if not has_write: for new_line in record_list: if new_line.startswith('backend'): write_file.write(new_line + ' ') else: write_file.write('%s%s ' % (' ' * 8, new_line)) has_write = True os.rename('ha.txt', 'ha_bak.txt') os.rename('ha_new.txt', 'ha.txt') os.remove('ha_bak.txt') #代码运行的时候就会把__name__赋值给__main__ #如果把代码当做模块导入,__name__就会被赋值为文件名 if __name__ == '__main__':#系统内置变量 #用户提示信息 msg = ''' 1:查询-fetch 2:添加-add 3:删除-remove 4:修改-change 5:退出-exit ''' #定义字典,实现提示信息与上面函数的对应关系,数字:函数名,不能加(),加上就是运行结果 #key定义成字符串的形式 menu_dic = { '1':fetch, '2':add, '3':remove, '4':change, '5':exit } #死循环,不断的跟用户去交互 #主逻辑 while True: print(msg) choice=input("操作>>:").strip()#用户选择 if choice == '2' or choice == '3': print("实例:{'backend':'www.baidu1.com','record':{'server':'2.2.2.2','weight':20,'maxconn':3000}}") if choice == '4': print("实例(旧内容,新内容):[{'backend':'www.baidu1.com','record':{'server':'1.1.1.1','weight':20,'maxconn':3000}},{'backend':'www.baidu1.com','record':{'server':'2.2.2.2','weight':20,'maxconn':4000}}]") #用户为空 or 用户超过可选范围 if len(choice) == 0 or choice not in menu_dic:continue#不在里面 if choice == '5':break #在用户输入完操作选项之后,输入 #data输入完之后,要把数据传入到上面的一个个功能,就是在函数名的地方加上data #所以上面的所有函数都要有参数,在函数名的()内都要加上data #用户输入的是字典的话,转换成字符串赋值给data data = input("数据>>:").strip() #如果用户输入不是1查询的话,因为默认是字符串的模式,所以要加引号 if choice != '1': #在下面执行的时候,再通过eval转换成字典,最后赋值给add,或者del功能 data = eval(data)#把用户输入的字符串转换成字符串原本包含的格式,最终赋值给data #用户执行操作 #menu_dic[choice]==fetch #menu_dic[choice]() == fetch() # menu_dic[choice](data) == fetch(data) #定义一个字典,通过字典的方式调用一个函数,这样函数名有变动,只要改字典的对应关系就可以了,这样主逻辑就不用动了 menu_dic[choice](data)