首页 > 代码库 > 修改haproxy配置文件

修改haproxy配置文件

HAproxy配置文件操作:

1. 根据用户输入输出对应的backend下的server信息

2. 可添加backend 和sever信息,如果存在则不添加,不修改

4. 可删除backend 和sever信息

5. 首先选择要进入哪种模式(查询,增加,删除),按q退出整个操作,在某种模式内时按b退出重新选择,不退出则 一直处于某种模式内

#Auther:He Jianhan

#查
def hanshu(names,fliens):
print(fliens[names].rstrip())
if names+1 < len (fliens):
if "bakend" not in fliens[names+1] and "backend" not in fliens[names+1]:
return hanshu(names+1,fliens)
#删除
def delets(names,fliens):
del fliens[names]
if names < len(fliens):
if "bakend" not in fliens[names]:
print("aaa")
return delets(names,fliens)



#打开文件
f = open("task","r",encoding="utf-8")
fliens = f.readlines()
come = True
while come:
choice = ["check","increase","delet"]
for n,c in enumerate(choice):
print(n,c)
number = input("你选择什么,输入编号==》")
if number.isdigit():
number = int(number)
if -1<number<len(choice):
if choice[number] == "check":
f = open("task", "r", encoding="utf-8")
fliens = f.readlines()
while come:
name = input("输入你要查找的内容")
name1 = "backend "+name+"\n"
name2 = "bakend "+name+"\n"
if name1 in fliens:
names = fliens.index(name1)
hanshu(names,fliens)
elif name2 in fliens:
names = fliens.index(name2)
hanshu(names, fliens)
elif name == "q":
come = False
elif name == "b":
break
else:
print("没有你要查找的内容")
f.close()

elif choice[number] == "increase":
f = open("task", "r", encoding="utf-8")
f_news = open("task", "a", encoding="utf-8")
while come:
name = input("输入你要增加的内容")
if name == "q":
come = False
elif name == "b":
break
else:
name = eval(name)
namebakend = "bakend "+name["bakend"]+"\n"
fliens = f.readlines()
if namebakend not in fliens:
f_news.write("bakend ")
f_news.write(name["bakend"]+"\n")
f_news.write(" ")
for key,values in name["record"].items():
f_news.write(" "+key+" "+str(values))
f_news.write("\n")
f_news.flush()
else:
print("之前输入过了")
f.close()
f_news.close()
elif choice[number] == "delet":
f = open("task", "r", encoding="utf-8")
while come:
fliens = f.readlines()
name2 = input("输入你要删除的内容")
name = "bakend " + name2 + "\n"
if name in fliens:
f_news = open("task", "w", encoding="utf-8")
names = fliens.index(name)
delets(names,fliens)
for fliens_s in fliens:
f_news.write(fliens_s)
f_news.close()
elif name2 == "q":
come = False
elif name2 == "b":
break
else:
print("没有你要查找的内容")
f.close()
else:
print("没有这个编号")
elif number == "q":
come = False
else:
print("输入错误")

修改haproxy配置文件