对haproxy文件做增删改查操作
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults 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 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 server 100.1.7.10 100.1.7.10 weight 10 maxconn 2000 server 100.1.7.19 100.1.7.19 weight 10 maxconn 2 frontend mysql bind *:3306 mode tcp log global default_backend mysqlserver backend mysqlserver server mysql1 10.1.1.110:3306 weight 20 maxconn 300 server mysql2 10.1.1.120:3306 weight 10 maxconn 200
# 代码
def new_list(all_list, l, temp, y): # 生成新的内容写入文件 L = all_list[0:temp + 1] + l + all_list[y:] write_file(L) def func_list(L): index, list_main, l = 0, [], [] # 显示主目录,并存入list_main中 for i in L: if i.startswith(' ') or len(i) == 0 or i == ' ': continue else: index += 1 print('%s、%s' % (index, i)) list_main.append(i) num = int(input('请输入操作代码:>>').strip()) if num not in range(1, 8): # 输入不在1-7间,需要重新输入 num = int(input('请输入操作代码1-7:>>').strip()) temp = L.index(list_main[num - 1]) # 查出指定主目录下的子项并存入l for y in range(temp + 1, len(L)): if L[y].startswith(' ' * 8): l.append(L[y]) else: break return l, temp, y #删除 def delete(all_list): l, temp, y = func_list(all_list) del_num = int(input('请输入操作代码:>>').strip()) # 删除掉定数字项 if len(l): l.pop(del_num - 1) new_list(all_list, l, temp, y) #修改 def revise(all_list): l, temp, y = func_list(all_list) re_num = int(input('请输入操作代码:>>').strip()) re_str = input('请输入操作值:>>').strip() re_str = ' ' * 8 + re_str + ' ' # 修改指定项内容 if len(l): l[re_num -1] = re_str new_list(all_list, l, temp, y) #增加 def add(all_list): l, temp, y = func_list(all_list) add_str = input('添加的内容>>').strip() s = ' ' * 8 + add_str + ' ' # 增加内容 if s not in l: l.append(s) new_list(all_list, l, temp, y) #查询 def reach(all_list): l, temp, y = func_list(all_list) q = 0 # 显示指定内容 for x in l: q += 1 print('%s、%s' %(q, x.strip())) # 写入文件 def write_file(L): with open('h1.txt', 'w') as f: for line in L: f.write(line) if __name__ == '__main__': with open('h.txt', 'r') as f: # 读取原配置文件 all_list = f.readlines() while True: print('1、查看') print('2、添加') print('3、删除') print('4、修改') print('5、退出') num = input('请输入操作代码:>>').strip() if num not in['1', '2', '3', '4', '5']: # 输入不在1-5间,需要重新输入 continue if num == '1': # 查看 reach(all_list) break elif num == '2': # 添加 add(all_list) break elif num == '3': # 删除 delete(all_list) break elif num == '4': # 修改 revise(all_list) break else: # 退出 break