python学习第三周作业:程序2:修改haproxy配置文件

#实现了查找输入输入:www.oldboy.org,查找backend记录;实现了添加功能;删除功能没有实现
Dict_backend={}
Dict_subrecord={}
Dict_record ={}
Dict_haproxy={}
Findflag =1
Existorg = []

#生成字典1
while True:
    select_option = input("请输入对用户对haproxy文件对应操作(查找输入find;增加输入add;删除输入delete:):")
    if(select_option == 'find'):
        def CreateBackendDict():
            for line in f:
                if"backend www.oldboy.org\n"in line:
                    s = line.strip()
                    s = s.split(' ')
                    Dict_backend[s[0]]=s[1]
                    #print(Dict_backend)#注释部分
                    s = (f.readline()).strip()
                    s = s.split(' ')
                    i = 0
                    j = 0
                    for i in range(int(len(s)/2)):
                        Dict_subrecord[s[j]] = s[j+1]
                        j += 2
                    Dict_backend["record"] = Dict_subrecord
                    #print(Dict_backend)#注释部分
                    return Dict_backend
        #查找功能实现如下:
        f = open("haproxy.txt",'r+')
        while Findflag:
            Dict_haproxy={}
            Dict_haproxy = CreateBackendDict()
            urlorg = input("请输入网址:")
            print(urlorg)
            if(Dict_haproxy != None):
                if Dict_haproxy['backend'] == urlorg:
                   print(Dict_haproxy["record"])
            if(Dict_haproxy == None):
                Findflag = 0
                print("haproxy配置文件中的,符合条件的服务器地址查找完成!!")
        f.close()
    elif(select_option == 'add'):
    #新建功能实现如下:
        arg = """{
                    'backend': 'www.oldboy.org',
                    'record':{
                        'server': '100.1.7.9',
                        'weight': 20,
                        'maxconn': 30
                    }
                }"""
        spacenum ='        '
        f = open("haproxy.txt",'a+')
        f_existproxy = open("existhaproxy.txt",'a+')
        f.seek(0)
        f_existproxy.seek(0)
        for line in f_existproxy:
            line = line.strip()
            Existorg.append(line)
        print("已经存在的org地址为:")
        print(Existorg)
        f.close()
        f_existproxy.close()
        while True:
            f = open("haproxy.txt", 'a+')
            f_existproxy = open("existhaproxy.txt", 'a+')
            s = input("请输入backend对应org地址:")
            if s in Existorg:
                print("此backend对应的org地址已经存在,请重新输入")
                continue
            elif s == 'q':
                break
            else:
                Existorg.append(s)
                f_existproxy.write(s + '\n')#输入org地址符合条件,则添加到已经存在列表
                f_existproxy.close()
                s = "backend" + ' '+s +'\n'
                f.write(s)
                server = input("请输入server对应地址:")
                weight = input("请输入weight对应的值:")
                maxconn = input("请输入maxconn对应的值:")
                s = spacenum + 'server'+' '+server +' ' + 'weight'+' '+'maxconn'+' '+maxconn+'\n'
                print(s)
                f.write(s)
                f.close()
    else:
        exit()

猜你喜欢

转载自www.cnblogs.com/fubaodemaPythonStudy/p/9697653.html