要求:
文件haproxy
1、查 输入:www.oldboy.org 获取当前backend下的所有记录
2、新建 输入: arg = {'backend': 'www.oldboy.org','record':{'server': '100.1.7.9','weight': 20,'maxconn': 30}}
实现:
1、文件open close readline write
2、字符串strip split
'''
1、查
输入:www.oldboy.org
获取当前backend下的所有记录 2、新建
输入:
arg = {'backend': 'www.oldboy.org','record':{'server': '100.1.7.9','weight': 20,'maxconn': 30}}
'''
def search(word,file='haproxy'):
fd=open(file,'r',encoding='utf-8')
data=fd.readline()
while data:
if word not in data or 'backend' not in data:
data=fd.readline()
else:
data=fd.readline()
while data \
and 'global' not in data \
and 'defaults' not in data \
and 'listen' not in data \
and 'frontend' not in data:
print(data)
data=fd.readline()
fd.close()
return True def addnew(word,file='haproxy'):
fd=open(file,'a',encoding='utf-8')
arg=eval(word)
for key in arg:
if key=='global' or key=='defaults' or key=='listen' or key=='frontend' or key=='backend':
fd.write(key+' '+arg[key]+'\n')
else:
data= arg[key]
wdata=''
for k in data:
wdata=wdata+' '+k+' '+str(data[k])
fd.write('\t'+wdata+'\n')
fd.close()
return #脚本主程序
flag=True
while flag:
choice=input("请输入您需要对haproxy文件进行的操作。\n1 查找 \n2 插入 \nq 退出\n")
if choice=='':
print("输入域名,可以找到backend下所有记录。")
sword=input("请输入您要查找的域名,如:www.oldboy.org:")
search(sword)
elif choice=='':
print("要插入的记录,写成字典格式。")
sword=input("请输入:")
addnew(sword)
elif choice=='q':
flag=False
else:
print("我不知道您要干什么。")