首页 > 代码库 > haproxy文件操作

haproxy文件操作

import os                                                       #导入os模块
def search(): #定义查找函数
with open(‘haproxy.txt‘,‘r‘) as f: #只读方式打开文件
value = http://www.mamicode.com/input(‘请输入您要查询的信息:‘)
backend = ‘backend %s‘ % value #字符串拼接
records = [] #定义空列表
tag = False #将标签设置为false
for line in f: #循环打开文件
if line.strip() == backend: #当line等于backend,将标签改为true
tag = True
continue #跳出当前循环
if line.startswith(‘backend‘): #当line以backend开头,跳出整个循环
break
if tag == True: #标签为true,将line去除首尾空值,加入到records列表中
records.append(line.strip())
print(records) #打印列表

def add(): #定义add函数
adds = input(‘请输入你要添加的内容:‘)
adds = eval(adds) #将adds转换为集合
backend = ‘\nbackend %s \n‘ % adds[‘backend‘] #拼接字符串
record = ‘ server {server} {server} weight {weight} maxconn {maxconn}\n‘.format(
server = adds[‘record‘][‘server‘],
weight = adds[‘record‘][‘weight‘],
maxconn = adds[‘record‘][‘maxconn‘]
)
records =[] #定义records空列表
with open(‘haproxy.txt‘,‘r‘) as r_file,\
open(‘haproxy_add.txt‘,‘w+‘) as w_file: #只读方式打开haproxy,生成新文件haproxy_add
tag = True #标签设置为true
for r_line in r_file: #循环文件
if r_line.strip() == backend.strip(): #当两个值相等,
tag = False #将标签设为false
records = r_file.readlines() #并将这一行的后面所有值加入到records列表中
w_file.write(backend) #将backend写入新文件
break #跳出for循环
w_file.write(r_line) #将r_line写入新文件
if tag == True: #当tag为true时
w_file.write(backend) #写入backend和record
w_file.write(record)
if tag == False: #当tag为false时
if record not in records: #如果record不在records列表内
records.insert(0,record) #把record的值加入到列表的第一个位置
for line in records: #遍历records列表
w_file.write(line) #将列表的值写入到新文件
os.rename(‘haproxy.txt‘,‘haproxy.addbak‘) #将文件备份
os.rename(‘haproxy_add.txt‘,‘haproxy.txt‘)


def remove(): #定义remove函数
removes = input(‘请输入你要删除的内容:‘)
removes = eval(removes) #将removes转换为集合
backend = ‘\nbackend %s\n‘ % removes[‘backend‘] #拼接字符串
record = ‘ server {server} {server} weight {weight} maxconn {maxconn}\n‘.format(
server = removes[‘record‘][‘server‘],
weight = removes[‘record‘][‘weight‘],
maxconn = removes[‘record‘][‘maxconn‘]
)
with open(‘haproxy.txt‘,‘r‘) as r_file,\
open(‘haproxy_remove.txt‘,‘w+‘) as re_file: #只读方式打开haproxy,生成新文件haproxy_remove
tag = True #定义标签为True
for r_line in r_file: #遍历文件
if backend.strip() == r_line.strip() or r_line.strip() == record.strip():
tag = False #当两个字符串有一个等于r_line时,将标签改为false
continue #跳出当前循环
if r_line.startswith(‘backend‘) or r_line.strip() != record.strip():
tag = True #当r_line开头为backend或者record不等于r_line时,将标签改为true
if tag == True: #当标签为true,写入新文件
re_file.write(r_line)
os.rename(‘haproxy.txt‘,‘haproxy.removbak‘) #备份文件
os.rename(‘haproxy_remove.txt‘,‘haproxy.txt‘)

def alter(): #定义alter函数
alters = input(‘请输入你要修改的原的内容:‘)
alters_new = input(‘请输入你要修改的新内容:‘)
alters = eval(alters) #将两个值转为字典
alters_new = eval(alters_new)
backend = ‘\nbackend %s\n‘ % alters[‘backend‘] #拼接字符串
record = ‘ server {server} {server} weight {weight} maxconn {maxconn}\n‘.format(
server=alters[‘record‘][‘server‘],
weight=alters[‘record‘][‘weight‘],
maxconn=alters[‘record‘][‘maxconn‘]
)
backends = ‘\nbackend %s\n‘ % alters_new[‘backend‘]
records = ‘ server {server} {server} weight {weight} maxconn {maxconn}\n‘.format(
server=alters_new[‘record‘][‘server‘],
weight=alters_new[‘record‘][‘weight‘],
maxconn=alters_new[‘record‘][‘maxconn‘]
)
with open(‘haproxy.txt‘, ‘r‘) as r_file, \
open(‘haproxy_alter.txt‘, ‘w+‘) as al_file: #只读方式打开haproxy,生成新文件haproxy_alter
for r_line in r_file: #遍历文件
if backend.strip() == r_line.strip(): #当原值为输入的原值,修改为新值
r_line = r_line.replace(backend,backends)
if r_line.strip() == record.strip():
r_line = r_line.replace(record,records)
al_file.write(r_line) #写入新文件
os.rename(‘haproxy.txt‘,‘haproxy.alterbak‘) #备份文件
os.rename(‘haproxy_alter.txt‘,‘haproxy.txt‘)


menu = [‘1.查找‘,‘2.新增‘,‘3.删除‘,‘4.修改‘,‘q.退出‘] #定义菜单
menu_func = {‘1‘:search, #定义功能字典
‘2‘:add,
‘3‘:remove,
‘4‘:alter,
}
while True: #开始循环
print(‘-----------------------------------‘)
for i in menu: #打印菜单
print(i)
choise = input(‘请选择您的操作方式(输入对应编号):‘)
if choise.isdigit(): #判断输入是否为数字
if int(choise) <= len(menu_func) and int(choise) >0: #判断输入数字是否在菜单内
menu_func[choise]() #调用功能字典内对应数字的函数
else: #为数字但不再菜单内,提示
print(‘请输入对应的编号!\n‘)
elif choise == ‘q‘: #输入q退出
print(‘退出成功!‘)
break
else: #不为数字提示
print(‘请输入对应的编号!\n‘)




作业有点乱,别喷我!!!!!我尽力了- -



haproxy文件操作