作业修改配置文件 查询,添加

本文详细介绍HAProxy的配置及操作流程,包括搜索、添加、删除和更新功能。通过实例演示如何使用HAProxy进行后端服务器的管理和配置,适用于运维人员和技术爱好者。
dic_function = {1:'search',2:'add',3:'delet',4:'update'}


for i in dic_function.items():
        print(i)
user_chose = input('请选择您的需求 :')

if user_chose == '1':
    seach_flage = False
    with open('haproxy', 'r', encoding='utf-8') as s:
        seach_f = input('请输入要查找的信息:')
        for line in s:
            if str(seach_f) in line:
                print(line)
                print(s.readline())
                lines = s.readlines()
                # print(type(lines))
                seach_flage =True
                break
        if seach_flage is False:
            print('不存在要查询的数据')


if user_chose == '2':
    dic = '''  {
                'backend': 'www.oldboy.org',
                'record':{
                         'server': '100.1.7.9',
                         'weight': 20,
                         'maxconn': 30
                         }
              } '''
    dic = eval(dic)
    chongfu_flag = False
    with open('haproxy', 'r', encoding='utf-8') as s:
        back = input('请输入backend:')
        for line in s:
            if back  in line:
                print('发现重复数据')
                chongfu_flag = True
                break
        if chongfu_flag is False:
            record_server = input('server:')
            record_weight = input('请输入weight:')
            record_maxconn = input('请输入maxconn:')
            dic['backend'] = back
            dic['record']['server'] = record_server
            dic['record']['weight'] = record_weight
            dic['record']['maxconn'] = record_maxconn
            print(dic)
            with open('haproxy', 'a', encoding='utf-8') as s:
                s.write('\nbackend:%s\n' % dic['backend'])
                s.write('\tserver%s' % dic['record']['server'])
                s.write('\tweight%s' % dic['record']['weight'])
                s.write('\tserver%s' % dic['record']['maxconn'])
                s.close()
        else:
            print("重复数据, 不添加")

##写代码要多加log文件或者多带点打印
##利用flag 跳出循环

转载于:https://www.cnblogs.com/PYlog/p/8650362.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值