key 知识点:函数的定义, 函数的递归调用, flag 标志位的使用,eval() 函数

#!C:\Program Files\Python35\bin
# -*- conding:utf-8 -*-
# author: Frank

def search_backend():
    search_list=[] #定义一个空列表,存放所查到backend的信息
    search_input = input("please input the backend which you want to search\n")
    with open ('Haproxy.cfg') as f:
        backend_flag = False #初始化标志位False
        for line in f:
            if line.strip()== 'backend %s'%search_input: #当匹配到所要查询的backend信息时,标志位为True,跳出本次循环,继续下一次循环
                backend_flag = True
                continue
            if backend_flag and line.strip().startswith('backend'): #上一个if语句,将标志位置为True,当再次遇到以’backend‘ 开头时
                                                                    #已经为下一个backend信息
                break
            if backend_flag and line.strip():  # 将上两个if条件除外的行追加到列表 search_list,即我们想要的行
                search_list.append(line.strip())
    if len(search_list) == 0:
        print("\033[31;1m%s not exit, please re_input\033[0m"%search_input)
        search_backend()
    else:
        for node in search_list:  #打印列表的每一行
            print(node)


def add_backend():
    args = '''{
                'backend':'%s',
                'record':{
                    'server':'%s',
                    'weight':'%s',
                    'maxconn':'%s'
                }
    }'''%(input('backend:'),input('server:'),input('weight:'),input('maxconn:'))
    b = eval(args)
    with open('Haproxy.cfg','a+',encoding='utf-8') as f:
        #print(' ',file=f)
        print('backend %s'%b['backend'],file=f,flush=True) #通过两个print, 将backend, 和下面的一行从格式上区别开
        print('\t\tserver %s weight %s maxconn %s'%(b['record']['server'],b['record']['weight'],b['record']['maxconn']),file=f,flush=True)

def delete_backend():
    # 删除正好和查询的思路相反,search 是将结果追加到列表里, delete是将所要删除的排除在外
    with open('Haproxy.cfg','r') as f_ori, open('Haproxy_new.cfg','a+') as f_new:
        delete_input=input("please enter backend which you want to delete:")
        backend_flag=True
        for line in f_ori:
            if line.strip() == 'backend %s'%delete_input:
                backend_flag=False
                continue
            if line.strip().startswith('backend') and line.strip() != 'backend %s' %delete_input:
                backend_flag=True
            if backend_flag: #除删除之外的所有行写到一个新的文件里
                f_new.write(line)

Mesg = '''1. Search backend
2. Add backend
3. delete backend
4. quit
'''
while True:
    print(Mesg)
    user_input = input("please input which do you want:")
    if user_input == "1":
        search_backend()
    elif user_input == "2":
        add_backend()
    elif user_input == "3":
        delete_backend()
    elif user_input == '4':
        exit()
    else:
        exit(print("Invaild input ,exit"))
View Code

相关文章:

  • 2022-12-23
  • 2021-09-12
  • 2021-11-20
  • 2021-12-25
  • 2022-12-23
  • 2021-12-10
  • 2021-11-05
猜你喜欢
  • 2021-04-16
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
相关资源
相似解决方案