知识点应用:strip()
tag
函数封装
文件操作
for循环
os模块
代码解耦
实例
tag = True
while tag:
print('level')
choice = input('level1>>:').strip()
while tag:
print('level2')
choice = input('level2>>:').strip()
if choice == 'quit':break #t退到上一层
if choice == 'quit_all': tag = False # 退出全部
while tag:
print('level3')
choice = input('level3>>>:').strip()
if choice == 'quit': break #t退到上一层
if choice == 'quit_all':tag = False #退出全部
未封装实例
import os #引入os模块
def fetch(data):
#查询
backend_data = 'backend %s' %data
with open('haproxy.conf','r') as read_f:
tag = False
ret = []
for read_line in read_f:
if read_line.strip() == backend_data: #这里说明匹配到想要的内容
tag = True #tag 的状态应当要改变
continue #跳过当前行
'''
退出规则
当tag = True 时 说明匹配到想要的内容
循环文件是一行一行的循环
当backend_data = global、defaults、listen、frontend时都不会进入if里面执行
当碰见一个backend时 tag变为True并跳出当前循环执行下一次循环
这时进来的是
server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30
server 2.2.2.7 2.2.2.7 weight 30 maxconn 4000
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000
程序不会执行
if tag and read_line.startswith('backend'):
break
tag为True状态没改变而继续执行
if tag:
print('\033[1;45m%s\033[0m' %read_line,end='') # 打印匹配到的内容
直到又遇到backend www.oldboy2.org 时
执行
if tag and read_line.startswith('backend'):
break
'''
if tag and read_line.startswith('backend'):
# tag = False #tag = False for循环依然会执行下去
break
if tag:
ret.append(read_line)
# print('\033[1;45m%s\033[0m' %read_line,end='') # 打印匹配到的内容
return ret
def add():
pass
def change(data):
#修改文件
backend = data[0]['backend'] #backend = www.oldboy1.org
backend_data = 'backend %s' %backend #得到:www.oldboy1.org
'''
拼接字符串 :' server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000'
'''
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'])
#print('用户想要修改的数据是:',old_server_record)
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'])
'''
data[0]是要被修改为data[0]的数据,类似data[0]是存在数据库中的,即将被更新为data[1]
'''
res = fetch(backend) #根据索引查询出数据
#print(res)
if not res or 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)
with open('haproxy.conf','r') as read_f,open('haproxy_new.conf','w') as writ_f:
tag = False
has_write = False
for read_line in read_f:
if read_line.strip() == backend_data:
'''
原理类似查询
找到匹配位置,改变状态tag = True
把修改的列表写入文件
'''
tag = True
continue
if tag and read_line.startswith('backend'):
tag = False
if not tag:
writ_f.write(read_line)
else:
if not has_write: #防止有多少个server写多少次
for record in res:
writ_f.write(record)
has_write = True
os.rename('haproxy.conf','haproxy.conf.bak') #备份源文件
os.rename('haproxy_new.conf','haproxy.conf') #修改新的文件名为原来的文件名
#os.remove('haproxy.conf.bak') #删除备份文件
def delete():
pass
#data[0]是要被修改为data[0]的数据,类似data[0]是存在数据库中的,即将被更新为data[1]
if __name__ == '__main__':
msg = '''
1:查询
2:添加
3:修改
4:删除
5:退出
'''
msg_dic = {
'1':fetch,
'2':add,
'3':change,
'4':delete,
}
while True:
print(msg)
choice = input('\033[1;43m请输入您的选项\033[0m:')
if not choice:continue
if choice == 5:break
data = input('请输入您的数据:').strip()
if choice != '1':
data = eval(data)
ret = msg_dic[choice](data) #拿到查询到的返回值
print(ret)
# [{'backend':'www.oldboy1.org','record':{'server':'2.2.2.5','weight':30,'maxconn':4000}},{'backend':'www.oldboy1.org','record':{'server':'2.2.2.4','weight':3,'maxconn':4000}}]
封装函数file_handler() 提取文件处理解耦部分
import os # 引入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:
if read_line.strip() == backend_data: # 这里说明匹配到想要的内容
tag = True # tag 的状态应当要改变
continue # 跳过当前行
'''
退出规则
当tag = True 时 说明匹配到想要的内容
循环文件是一行一行的循环
当backend_data = global、defaults、listen、frontend时都不会进入if里面执行
当碰见一个backend时 tag变为True并跳出当前循环执行下一次循环
这时进来的是
server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30
server 2.2.2.7 2.2.2.7 weight 30 maxconn 4000
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000
程序不会执行
if tag and read_line.startswith('backend'):
break
tag为True状态没改变而继续执行
if tag:
print('\033[1;45m%s\033[0m' %read_line,end='') # 打印匹配到的内容
直到又遇到backend www.oldboy2.org 时
执行
if tag and read_line.startswith('backend'):
break
'''
if tag and read_line.startswith('backend'):
# tag = False #tag = False for循环依然会执行下去
break
if tag:
ret.append(read_line)
# print('\033[1;45m%s\033[0m' %read_line,end='') # 打印匹配到的内容
return ret
elif type == 'change':
with open('haproxy.conf', 'r') as read_f, open('haproxy_new.conf', 'w') as writ_f:
tag = False
has_write = False
for read_line in read_f:
if read_line.strip() == backend_data:
'''
原理类似查询
找到匹配位置,改变状态tag = True
把修改的列表写入文件
'''
tag = True
continue
if tag and read_line.startswith('backend'):
tag = False
if not tag:
writ_f.write(read_line)
else:
if not has_write: # 防止有多少个server写多少次
for record in res:
writ_f.write(record)
has_write = True
os.rename('haproxy.conf', 'haproxy.conf.bak') # 备份源文件
os.rename('haproxy_new.conf', 'haproxy.conf') # 修改新的文件名为原来的文件名
os.remove('haproxy.conf.bak') #删除备份文件
def fetch(data):
# 查询
backend_data = 'backend %s' % data
return file_handler(backend_data)
def add():
pass
def change(data):
# 修改文件
backend = data[0]['backend'] # backend = www.oldboy1.org
backend_data = 'backend %s' % backend # 得到:www.oldboy1.org
'''
拼接字符串 :' server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000'
'''
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'])
# print('用户想要修改的数据是:',old_server_record)
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'])
'''
data[0]是要被修改为data[0]的数据,类似data[0]是存在数据库中的,即将被更新为data[1]
'''
res = fetch(backend) # 根据索引查询出数据
# print(res)
if not res or 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)
return file_handler(backend_data,res = res,type = 'change')
def delete():
pass
# data[0]是要被修改为data[0]的数据,类似data[0]是存在数据库中的,即将被更新为data[1]
if __name__ == '__main__':
msg = '''
1:查询
2:添加
3:修改
4:删除
5:退出
'''
msg_dic = {
'1': fetch,
'2': add,
'3': change,
'4': delete,
}
while True:
print(msg)
choice = input('\033[1;43m请输入您的选项\033[0m:')
if not choice: continue
if choice == 5: break
data = input('请输入您的数据:').strip()
if choice != '1':
data = eval(data)
ret = msg_dic[choice](data) # 拿到查询到的返回值
print(ret)
#更新文件
# [{'backend':'www.oldboy1.org','record':{'server':'101.1000.7.9','weight':20,'maxconn':30}},{'backend':'www.oldboy1.org','record':{'server':'2.2.2.7','weight':213,'maxconn':720}}]