练习:修改haproxy文件配置
要求:
# 修改haproxy配置文件
# 查询:输入网址获取当前backend下的所有记录
# 增加:输入字典增加一条记录
# 删除:输入字典删除该记录
目前完成的版本有一个比较大的问题就是增加删除的时候如果不按格式输入代码会报错,后续再改进吧
1global2 log 127.0.0.1 local2
3 daemon
4 maxconn 256
5 log 127.0.0.1 local2 info
6defaults
7 log global
8 mode http
9 timeout connect 5000ms
10 timeout client 50000ms
11 timeout server 50000ms
12 option dontlognull
13
14 listen stats :8888
15 stats enable
16 stats uri /admin
17 stats auth admin:1234
18
19frontend oldboy.org
20 bind 0.0.0.0:80
21 option httplog
22 option httpclose
23 option forwardfor
24 log global
25 acl www hdr_reg(host) -i www.oldboy.org
26 use_backend www.oldboy.org if www
27
28backend www.oldboy.org
29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
haproxy文档
1# 修改haproxy配置文件2# 查询:输入网址获取当前backend下的所有记录
3# 增加:输入字典增加一条记录
4# 删除:输入字典删除该记录
5
6import sys
7
8
9def info():
10"""提取当前配置文件中所有backend网址信息"""
11 backend = []
12 with open("haproxy", "r") as f:
13for line in f:
14if line.startswith("backend"):
15 line = line.strip()
16 backend.append(line.partition("")[2])
17return backend
18
19
20def search(website):
21"""输入网址查找其具体信息"""
22 info_backend = []
23if website in info():
24 with open("haproxy", "r") as f:
25for line in f:
26if line.strip() == "backend"+""+website:
27 f1 = f.readline()
28while f1.startswith(" server"):
29 info_backend.append(f1.strip())
30 f1 = f.readline()
31print(f"33[31m{website}条目信息:33[0m")
32for i, items in enumerate(info_backend):
33print(i+1, items.strip("""))
34else:
35print(f"{website} 条目不存在")
36return info_backend
37
38
39def add(add_dic):
40"""添加条目"""
41 add_dic = eval(add_dic)
42 website = add_dic["backend"]
43 record = add_dic["record"]
44if website in info():
45print(f"33[31m{website}已存在33[0m")
46else:
47 with open("haproxy", "a") as f:
48 f.write("
"+"backend"+""+website+"
")
49 f.write(""+"server"+""+record["server"]+""+record["server"]+"")
50 f.write("weight"+""+str(record["weight"])+"")
51 f.write("maxconn"+""+str(record["maxconn"]))
52print(f"33[31mbackend {website} 条目已添加33[0m")
53
54
55def delete(del_dic):
56"""删除条目"""
57 del_dic = eval(del_dic)
58 website = del_dic["backend"]
59if website in info():
60 with open("haproxy", "r") as f:
61 with open("haproxy_new.txt", "w+") as f1:
62for line in f:
63if line.strip() == "backend"+""+del_dic["backend"]:
64 line = f.readline()
65while line.startswith(" server"):
66 line = f.readline()
67continue
68else:
69 f1.write(line)
70print(f"{website} 条目删除成功")
71 with open("haproxy", "w") as f:
72 with open("haproxy_new.txt", "r") as f1:
73for line in f1:
74 f.write(line)
75else:
76print(f"{website} 条目不存在")
77return
78
79
80print("33[31m欢迎进入haproxy配置系统33[0m".center(50, ""))
81while True:
82print("-".center(50, "-"))
83print("当前backend网址信息如下:")
84for j, backend in enumerate(info()):
85print(j+1, backend)
86print("-".center(50, "-"))
87print("1.信息查询", "
", "2.增加条目", "
", "3.删除条目", sep="")
88 choice1 = input("输入序号进入配置界面(输入q退出系统):")
89if choice1.isdigit():
90 choice1 = int(choice1)
91if choice1 in range(1, 4):
92if choice1 == 1:
93while True:
94 choice2 = input("请输入网址进行查询(例:www.baidu.com)(b返回q退出):")
95if choice2 == "b":
96break
97elif choice2 == "q":
98print("系统退出")
99 sys.exit()
100else:
101 search(choice2)
102elif choice1 == 2:
103while True:
104 choice3 = input("请输入条目字典(b返回q退出):")
105if choice3 == "b":
106break
107elif choice3 == "q":
108print("系统退出")
109 sys.exit()
110else:
111 add(choice3)
112elif choice1 == 3:
113while True:
114 choice4 = input("请输入添加条目字典(b返回q退出):")
115if choice4 == "b":
116break
117elif choice4 == "q":
118print("系统退出")
119 sys.exit()
120else:
121 delete(choice4)
122else:
123print("无此选项,请正确输入!")
124elif choice1 == "q":
125print("系统退出")
126 sys.exit()
127else:
128print("格式错误,请重新输入!")
129continue
haproxy代码
以上是 练习:修改haproxy文件配置 的全部内容, 来源链接: utcz.com/z/530769.html