1 global
2 log 127.0.0.1 local2
3 daemon
4 maxconn 256
5 log 127.0.0.1 local2 info
6 defaults
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
19 frontend 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
28 backend www.oldboy1.org
29 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
30 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
31 server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
32 backend www.oldboy2.org
33 server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
34 backend www.oldboy20.org
35 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
36
37
38
39
40
41 需求:
42
43 查询功能:
44 当用户输入www.oldboy1.org用户信息后,打印对应的server信息:
45 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
46 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
47 server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
48
49 增加功能
50 当用户输入{"backend":"www.oldboy20.org","record":server 1111 weight 2222 maxconn 3333}
51 在www.oldboy20.org下增加一条server信息,即文件中:
52 backend www.oldboy20.org
53 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
54 server 1111 weight 2222 maxconn 3333
55
56 删除功能
57 当用户输入{"backend":"www.oldboy20.org","record":server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333}}
58 会将www.oldboy20.org下的server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333删除,
59 backend www.oldboy20.org
60 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
61
62 当用户输入{"backend":"www.oldboy2.org","record":"server 0000 weight 0000 maxconn 0000"}
63 backend www.oldboy2.org
64 server 0000 weight 0000 maxconn 0000
65
代码需求
1 def menu():
2 '执行功能列表'
3 print(
4 '''
5 ***********
6 1 查询
7 2 增加
8 3 删除
9 4 修改
10 ***********
11 '''
12 )
13 def demand():
14 '查询功能'
15 while 1:
16 l=[] #创建一个空列表,装打印的信息
17 m = input('''
18 请输入如www.oldboy1.org用户信息:
19 '''
20 ).strip()
21 #查询功能:
22 # 当用户输入www.oldboy1.org用户信息后,打印对应的server信息:
23 # server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
24 # server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
25 # server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
26 flag = False
27 with open("test",encoding="utf8") as demand_test:
28 for line in demand_test:
29 if line.startswith("backend") and m in line:#匹配输入信息,打开开关
30 flag = True
31 continue #跳过不打印
32 if line.startswith("backend") and flag == True:#打印完信息,关闭开关
33 break
34 if flag == True:#打印用户信息
35 l.append(line)#将用户信息追加进空列表
36 for i in l:
37 print(i.strip())#打印列表
38 def add():
39 '增加功能'
40 while True:
41 m = input('''
42 请输入如{"backend":"www.oldboy20.org",
43 "record":"server 1111 weight 2222 maxconn 3333"}的增加信息:
44 '''
45 )
46 #增加功能
47 # 当用户输入{"backend":"www.oldboy20.org","record":server 1111 weight 2222 maxconn 3333}
48 # 在www.oldboy20.org下增加一条server信息,即文件中:
49 # backend www.oldboy20.org
50 # server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
51 # server 1111 weight 2222 maxconn 3333
52 m = eval(m) #将字符串转成字典
53 with open("test", encoding="utf8") as add_test ,open("test2", encoding="utf8", mode="a") as add_increase:
54 for line in add_test:
55 add_increase.write(line) #将内容全部写入另一个文件
56 line = " "+m["record"]
57 add_increase.write(line)#将修改的内容追加进文件中
58 import os
59 os.rename("test", "test_bak")
60 os.rename("test2", "test") # 修改文件名
61 os.remove("test_bak")
62 def delete():
63 '删除功能'
64 flag = False
65 while flag ==False:
66 m = input('''
67 请输入如{"backend":"www.oldboy20.org",
68 "record":"server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333"}位置信息:
69 ''')
70 #删除功能
71 #当用户输入{"backend":"www.oldboy20.org","record":server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333}}
72 #会将www.oldboy20.org下的server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333删除,
73 m = eval(m) #将字符串转换成字典
74 with open("test", encoding="utf8") as f_read, open("test7", encoding="utf8", mode="w") as f_del:
75 for line in f_read:
76 if line.strip() ==m["record"]: #需要删除的内容跳过不写入
77 continue
78 else:
79 flag = True
80 f_del.write(line)
81 import os
82 os.rename("test", "test_bak")
83 os.rename("test7", "test")
84 os.remove("test_bak")
85 def revise():
86 '修改功能'
87 while 1:
88 m = input('''
89 请输入如{"backend":"www.oldboy2.org","record":"server 0000 weight 0000 maxconn 0000"}的修改信息
90 ''')
91 # 当用户输入{"backend":"www.oldboy2.org","record":"server 0000 weight 0000 maxconn 0000"}
92 # backend www.oldboy2.org
93 # server 0000 weight 0000 maxconn 0000
94 m = eval(m)#将字符串转换成字典
95 with open("test",encoding="utf8") as revise_test ,open("test4",encoding="utf8",mode="w") as alter:
96 for line in revise_test:
97 if line.startswith("backend") and m["backend"] in line:
98 line = line+" "+m["record"]+"
"#在需要修改的行的前一行后面加上修改内容
99 if line.strip() == "server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000":#需要修改的行跳过
100 continue
101 alter.write(line)
102 import os
103 os.rename("test","test_bak")
104 os.rename("test4","test")
105 os.remove("test_bak")
106
107 def main():
108 while True:
109 menu()
110 choice = int(input("请输入操作的序号:").strip())
111 if choice == 1:
112 demand()
113 break
114 if choice == 2:
115 add()
116 break
117 if choice == 3:
118 delete()
119 break
120 if choice == 4:
121 revise()
122 break
123 else:
124 print("无此选项")
125
126 main()