配置文件

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.9 100.1.7.9 weight 20 maxconn 3000
        server 100.1.2.9 100.1.7.9 weight 20 maxconn 3000
        server 100.1.2.129 100.1.7.9 weight 20 maxconn 3000
backend buy.oldboy.org
        server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
配置文件
 1 def fetch(backend):
 2     result = []
 3     f = "lv"
 4     with open('ha.conf','r',encoding='utf -8') as f:
 5         for line in f:
 6             if line.strip() == "backend %s" %backend:#
 7                 f = "ho"
 8                 continue
 9             if f == "ho":
10                 if line.strip().startswith('backend'):
11                     f = "lv"
12                 else:
13                     if line.strip():
14                         result.append(line.strip())
15     return result
16 
17 ret = fetch("www.oldboy.org")
18 print(ret)
19 
20 def add1(backend,record):
21     f = "lv"
22     with open("ha.conf","r",encoding="utf-8") as f1,open("new.conf","w",encoding="utf-8") as f2:
23         for line in f1:
24             if f == "lv" and line != "backend %s" %backend:
25                 f2.write(line)
26             if f == "lv" and line == "backend %s" %backend:
27                 f2.write(line)
28                 f = "ho"
29                 f2.write(line)
30             if f == "ho" and line != "backend %s" %backend:
31                 if line.strip().startswith("backend"):
32                     # if record not in fetch(backend):
33 
34                     f2.write(" "*8 +record+"
")
35                     f = "lv"
36                 f2.write(line)
37 add1('www.oldboy.org','server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000')
 1 def fetch(backend):
 2     result = []
 3     f = "绿灯"
 4     with open('ha.conf','r',encoding='utf -8') as f:
 5         for line in f:
 6             if line.strip() == "backend %s" %backend:#
 7                 f = "红灯"
 8                 continue
 9             if f == "红灯":
10                 if line.strip().startswith('backend'):
11                     f = "绿灯"
12                 else:
13                     if line.strip():
14                         result.append(line.strip())
15     return result
16 
17 ret = fetch("www.oldboy.org")
18 print(ret)
19 
20 
21 def add(backend,record):
22     import os
23     record_list = fetch(backend)
24     record_list.append(record)
25     print(record_list)
26     f = '绿灯'
27     with open('ha.conf','r',encoding='utf-8') as old, open('new_1.conf','w',encoding='utf-8') as new:
28         for line in old:
29             if f == '绿灯' and line.strip() != 'backend %s' %backend:
30                 new.write(line)
31             if f == "绿灯" and line.strip() == 'backend %s' %backend:
32                 f = '红灯'
33                 new.write(line)
34             if f == '红灯' and line.strip() != 'backend %s' %backend :
35                 if len(line.strip()) == 0:
36                     continue
37                 if line.strip().startswith('backend'):
38                     if record not in fetch(backend):
39                         new.write(" "*8 +record+"
")
40                     f = '绿灯'
41                 new.write(line)
42     if os.path.isfile("ha.conf.bak"):
43         os.remove("ha.conf.bak")
44     os.rename("ha.conf","ha.conf.bak")
45     os.rename("new_1.conf","ha.conf")
46 
47 add('www.oldboy.org', 'server 100.1.2.129 100.1.7.9 weight 20 maxconn 3000')
原文地址:https://www.cnblogs.com/kongqi816-boke/p/5544368.html