需求:修改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
时间: 2024-10-03 22:29:13