淮南草的博客

淮南草的博客

Python3 作业三_修改haproxy配置

作业未完成

'''
Created on 
@author: hcl
'''

arg = {
        'bakend':'www.oldboy.org',
        'record':{
            'server':'100.1.7.9',
            'weight':20,
            'maxconn':30
            }
    }
haproxy_local = r'F:\eclipse-workspace\algorithm\src\Python Train Data\haproxy.txt'

def op_item(op):
    if op == 'find' :
        host_name = input("Please input your host name:")
        find_flag = 0
        with open(haproxy_local,'r') as f:
            for item in f:   
                if find_flag == 1 and item.startswith('\t'):
                    print(item.strip('\n'))
                else:
                    find_flag = 0
                if ('backend'+' '+host_name == item.strip('\n')):                
                    print(item.strip('\n'))    
                    find_flag = 1
    elif op == 'add':             
        add_host = input("Please input your host name:")
        with open(haproxy_local,'r+') as f:
            for item in f:  
                if ('backend'+' '+add_host == item.strip('\n')):                
                    print(item.strip('\n'))
                    add_server = ''
                    for keys in arg['record'].keys():
                        add_server += keys+' '
                        add_server += str(arg['record'][keys]) +' '
                    f.write('\n\t'+add_server)
                    f.flush()
                    print("write success")
                        
                            
                        
while True:
    input_op = input("Please input your operation(add,delete,find):")
    op_item(input_op)

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhuisaozhang1292/article/details/79959586
上一篇Python3函数
下一篇Python3装饰器
想对作者说点什么? 我来说一句

keepdalive 配置

2018年04月28日 2KB 下载

没有更多推荐了,返回首页

关闭
关闭