码迷,mamicode.com
首页 > 其他好文 > 详细

09.小实战03

时间:2017-09-26 19:16:45      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:break   please   temp   ken   arc   python   auth   reg   back   

需求:修改haproxy配置文件

代码实现:

# 查询
def serach(str):
    with open("ha.cnf", ‘r‘) as f:
        flag = False
        for line in f:
            if flag == True:
                return line
                break
            if line.strip() == "backend %s" % str:
                flag = True
    return False

# 插入
def insert(str):
    istr = eval(str)  # 将字符串转字典
    if serach(istr[‘backend‘]) == False:
        with open("ha.cnf", "a") as f:
            f.write(‘‘‘\nbackend %s \n\t\tserver %s weight %s maxconn %s‘‘‘ % (istr[‘backend‘], istr[‘server‘], istr[‘weight‘], istr[‘maxconn‘]))
    else:
        print("this line have exist!")

# 删除
def delete(str):
    if serach(str) != False:
        with open("ha.cnf", "r") as f, open("ha.temp", "w") as ftemp:
            flag = False
            for line in f:
                if line.strip() == "backend %s" % str:
                    flag = True
                else:
                    if flag != True:
                        ftemp.write(line)
                    else:
                        flag = False

# 选择操作
select = input("please select:\n 1.search 2.insert 3.delete\n")

if select == "1":
    node = input("\nnode:")
    a = serach(node)
    print(a)
elif select == "2":
    insert("{‘backend‘: ‘www.baidu.com‘, ‘server‘: ‘1.1.1.1‘, ‘weight‘: ‘20‘, ‘maxconn‘: ‘3000‘}")
elif select == "3":
    a = input("\nnode:")
    delete(a)

 配置文件:ha.cnf

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 weight 20 maxconn 3000

backend abc
        server 1.1.1.1 weight 20 maxconn 3000

backend www.baidu.com 
        server 1.1.1.1 weight 20 maxconn 3000

 

09.小实战03

标签:break   please   temp   ken   arc   python   auth   reg   back   

原文地址:http://www.cnblogs.com/boreassun/p/7598027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!