import os
def file_handler(backend_data, res=None, type='fetch'):#文件处理
if type == 'fetch':
with open('haproxy.conf', 'r')as read_f:
tag = False
ret = []
for read_line in read_f: # b遍历文件的每一行
if read_line.strip() == backend_data: # 进行查询
tag = True
continue
if tag and read_line.startswith('backend'): # 判断结束,新的backend开头就退出
# tag=False
break
if tag:
print('\033[1;50m%s\033[0m' % read_line, end='') # 这就是需要找的,直接打印出看到的语句
ret.append(read_line)
return ret
elif type == 'change':
with open('haproxy.conf', 'r')as read_f, \
open('haproxy.conf_new', 'w') as write_f:
tag = False
has_write = False
for read_line in read_f: # server
if read_line.strip() == backend_data: # 找到backend
tag = True # 已经读到backend www.oldboy1.org,再往下读取都是server,拉响警包
continue
if tag and read_line.startswith('backend'): # 遇到下一个backend要关闭警报,
tag = False
if not tag: # 如果警报是关闭状态
write_f.write(read_line)
else: # if tag == True:
if not has_write:
for record in res: # res是查询时返回的列表
write_f.write(record) # 写的是列表内容,内容只有server信息,不包含backend
has_write = True
os.rename('haproxy.conf', 'haproxy.conf.bak') # 把haproxy.conf改名-->>>haproxy.conf.bak
os.rename('haproxy.conf_new', 'haproxy.conf')
os.remove('haproxy.conf.bak')
def fetch(data): # 查询功能
# print('\033[1;45m这是查询功能\033[0m')
# print('\033[1;45m用户数据是\033[0m', data)
backend_data = 'backend %s' % data # 拼接字符串
return file_handler(backend_data)
def add():
pass
def change(data): # 修改功能
# print('这是修改功能')
# print('用户输入的数据是',data)
backend = data[0]['backend'] # 文件中要修改的一条记录 #backend www.oldboy1.org
backend_data = 'backend %s' % backend # 拼接结果 backend www.oldboy1.org
old_server_record = '%sserver %s %s weight %s maxconn %s\n' % (' ' * 8, data[0]['record']['server'],
data[0]['record']['server'],
data[0]['record']['weight'],
data[0]['record']['maxconn'],)
new_server_record = '%sserver %s %s weight %s maxconn %s\n' % (' ' * 8, data[1]['record']['server'],
data[1]['record']['server'],
data[1]['record']['weight'],
data[1]['record']['maxconn'],)
print('用户想要修改的记录', old_server_record)
res = fetch(backend) # fetch('www.oldboy1.org')
print('来自change函数--->>>', res)
if not res or old_server_record not in res:
return '你要修改的记录不存在!!!'
# if not res:#判断查询空
# return '修改的记录不存在!!!'
# if old_server_record not in res:
# return '你要修改的几率不存在!!!'
else: # 修改的记录存在
index = res.index(old_server_record) # 取出列表中的索引
res[index] = new_server_record
res.insert(0, '%s\n' % backend_data) # 为新创建的文件填写backend开头
file_handler(backend_data, res=res,type='change')
def delete():
pass
if __name__ == '__main__': # 系统变量 __name__ 就等于 __main__,(直接打mian)
msg = '''#提示信息
1.查询
2.添加
3.修改
4.删除
5.退出
''' # 提示信息
msg_dic = {
'1': fetch, # 只写函数名就行
'2': add,
'3': change,
'4': delete,
# '5':exit
}
while True:
print(msg)
choice = input('请输入你的选项:').strip()
if not choice: continue # 判断如果输入空值,choice自带boor值
if choice == '5': break
data = input('请输入你的数据').strip() # .strip()默认去除空格和回车,也可以指定去除目标
if choice != '1':
data = eval(data) # 根据修改功能改的将数据由字符串改为列表,提取数据结构
res = msg_dic[choice](data) # 利用函数字典调用函数,choice就相当于函数字典中的key
print('最终的结果是---->>>', res)
[{'backend': 'www.oldboy1.org', 'record': {'server': '2.2.2.4', 'server': '2.2.2.4', 'weight': 20, 'maxconn': 3000}},{'backend': 'www.oldboy1.org', 'record': {'server': '2.2.2.5', 'server': '2.2.2.5', 'weight': 30, 'maxconn': 4000}}]
Python的time与random等常用模块与包4-程序的解耦
最新推荐文章于 2024-07-10 19:27:42 发布