zoukankan      html  css  js  c++  java
  • 对haproxy文件进行增删改查

    1、文件内容

    global
            log 127.0.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    defaults
            log global
            mode http
            timeout connect 5000ms
            timeout client 50000ms
            timeout server 50000ms
            option  dontlognull
    
    listen stats :8888
            stats enable
            stats uri       /admin
            stats auth      admin:1234
    
    frontend oldboy.org
            bind 0.0.0.0:80
            option httplog
            option httpclose
            option  forwardfor
            log global
            acl www hdr_reg(host) -i www.oldboy.org
            use_backend www.oldboy.org if www
    
    backend www.oldboy.org
            server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
            server 100.1.7.19 100.1.7.19 weight 40 maxconn 4000
    
    backend buy.oldboy.org
            server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000

    2、代码实现

    filename = 'text_filehaproxy'
    def fetch(backend):
        '''获取文件数据'''
        with open(filename, 'r') as f:
            result = []
            flag = False
            for line in f:
                if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
                    flag = True
                    continue
                elif flag and line.startswith('backend'):
                    break
                elif flag and line.strip():
                    result.append(line.strip())
        return result
    
    #测试
    ret = fetch("www.oldboy.org")
    print(ret)
    
    
    def add(backend,record):
        '''增加文件数据'''
        record_list = fetch(backend)
        #先检查backend是否存在
        if not record_list:
            #backend不存在
            with open(filename,'r') as old,open(r'text_file
    ew','w') as new:
                for line in old:
                    new.write(line)
                new.write("
    
    backend " + backend)
                new.write("
    " + " "*8 + record)
        else:
            #backend存在,判断记录是否存在
            if record in record_list:
                #record存在
                print("记录已存在")
            else:
                #backend存在,record不存在
                record_list.append(record)
                with open(filename,'r') as old,open(r'text_file
    ew2','w') as new2:
                    flag = False
                    for line in old:
                        if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
                            #找到backend www.oldboy.org
                            flag = True
                            new2.write(line)#把backend行加入新文件
                            for new_record in record_list:
                                #把record列表中的元素添加到新文件
                                new2.write(" " * 8 + new_record + "
    ")
                            continue
                        if flag and line.startswith('backend'):
                            flag = False
                            new2.write("
    " + line)
                            continue
                        if flag == False:
                            new2.write(line)
    
    #测试
    bk = "www.oldboy.org"
    rd = "server 34.1.7.25 100.1.7.44 weight 99 maxconn 67"
    add(bk,rd)
    bk = "test.oldboy.org"
    rd = "server 50.1.7.55 100.1.7.45 weight 99 maxconn 89"
    add(bk,rd)
    
    
    def delete(backend,record):
        '''删除文件数据'''
        #先判断backend是否存在
        record_list = fetch(backend)
        if not record_list:
            print("记录不存在")
        else:
            with open(filename,'r') as f,open(r'text_file
    ew3','w') as new3:
                if record in record_list:
                    for line in f:
                        if line.strip() == record:
                            continue
                        else:
                            new3.write(line)
    
    #测试
    bk = "www.oldboy.org"
    rd = "server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000"
    delete(bk,rd)
    

      

  • 相关阅读:
    超棒的jQuery矢量地图生成插件 JQVAMP
    签名一年过期 项目导入出现 红叉叉
    invalid commandline parameter: Files\Android\androidsdk\tools/emulatorarm.exe 错误
    线性布局 相对布局 参数
    触摸事件 按下 移动 弹起
    Activity service 通信
    android view的setVisibility方法值的意思
    TextView 支持 html 图片显示
    布局动态添加 相对布局
    存储数据 SharedPreferences
  • 原文地址:https://www.cnblogs.com/charliedaifu/p/10090220.html
Copyright © 2011-2022 走看看