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.baidu.org
29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
haproxy configuration file
1 #!/usr/bin/env python
2 # =*- coding:UTF-8 -*-
3
4 # Find
5 def search(website ):
6 reault_list = []#Store query results
7 search_flag = False #Define query result flag
8
9 with open('haproxy','r',encoding= 'utf-8') as f:
10 for line in f:#The if statement is performed in sequence, and the current condition is judged , Go to the next judgment
11 if 'backend {}'.format(website) == line. strip():#Find backend www.oldboy.org and start intercepting
12 search_flag = True
13 if line.strip().startswith( 'backend') and 'backend { }'.format(website) != line.strip() :# span>find the end of the next backend www.xxx.com part, intercept part of it
14 search_flag = False
15 if search_flag:
16 reault_list.append(line .strip())
17 print(reault_list)
18 f.close()
19 if reault_list == []:
20 print('Sorry, the URL you query does not exist!' span>)
21 else:
22 return reault_list
23
24 # Increase
25 def add(website):
26 arg = eval(website) #turn the input string into a dictionary
27 backend_title = 'backend {}'.format(arg['backend'])# The field to be inserted into the backend
28 record_title = arg['record']
29 context_record = 'server {0}{0} weight {1} maxconn {2}'. 30 format(record_title['server'],record_title['weight'],record_title['maxconn'])
31 add_flag = True#Set the new flag bit
32 with open('haproxy', 'r+', encoding='utf-8') as f:
33 for line in f:
34 if line.strip() ==< span style="color: #000000;"> backend_title:
35 print("The URL you added already exists!")
36 add_flag = False
37 if add_flag:
38 f.write(' {}'.format(backend_title) )
39 f.write(' {}'. format(context_record))
40 f.close()
41
42 # Delete
43 def delete(website):
44 delete_flag = False #Set the delete flag bit
45 delete_result = True
46 with open('haproxy', 'r', encoding='utf-8') as f, 47 open('haproxy_bak', 'w') as f1:
48 for line in f:
49 if 'backend {}'.format(website) == line.strip():
50 delete_flag = True
51 delete_result = False
52 continue
53 if line.strip().startswith('backend') and line.strip() != website:
54 delete_flag = False
55 if not delete_flag:
56 f1.write(line)
57 if delete_result:
58 print("The URL you deleted does not exist!" )
59 with open('haproxy', 'w') as f, 60 open('haproxy_bak ', 'r', encoding='utf-8') as f1:
61 for line in f1:
62 f.write(line)
63
64 # Edit
65 def update(website):
66 arg = eval(website)
67 # print(arg)
68 backend_title = "backend {}".format(arg['backend']) # To insert the entire backend field
69 # print(backend_title)
70 record_title = arg["record"]
71 context_record = "server {0} {0} weight {1} maxconn {2}". 72 format(record_title['server'], record_title['weight'], record_title['maxconn'])
73 # print(context_record)
74
75 update_flag = False #Set the modification flag
76 update_re = False #Set repeated modification bits
77
78 with open('haproxy', 'r', encoding='utf-8') as f, < /span> 79 open('haproxy_bak', 'w') as f1:
80 for line in f:
81 if line.strip() ==< span style="color: #000000;"> backend_title:
82 update_flag = True
83 continue #If the modification backend_title appears, set the modification flag to True.
84 if line.strip().startswith('backend') and line.strip() != backend_title:
85 update_flag = False #Capture the next backend_title, and set the modification flag to False.
86 if not update_flag:
87 f1.write(line)
88 if update_flag and not update_re: #The modification flag bit is True, and it has not been modified
89 f1.write(' {}'.format(backend_title))
90 f1.write(' {} '.format(context_record))
91 update_re = True
92
93 with open('haproxy', 'w') as f, 94 open('haproxy_bak ', 'r span>', encoding='utf-8') as f1:
95 for line in f1:
96 f.write(line)
97
98
99
100
101 while True:
102 print('Welcome to haproxy configuration program'.center (50,'-' ))
103 print( ' 1 Query ')
104 print(' 2 Add new ')
105 print(' 3 Delete ')
106 op_haproxy = input('Select the ID to enter the mode-":' )
107 if op_haproxy == '1':
108 website = input('Please enter the URL you want to query: '
109 ' E.g. www.oldboy.org')
110 search(website)
111 elif op_haproxy == '2':
112 website = input("Please enter the URL configuration to be added:"
113 " For example: {'backend':'www.baidu.com','record': {'server': '100.1.7.8',"
114 " 'weight': 20,'maxconn': 3000}} ")
115 add(website)
116 elif op_haproxy == '3':
117 website = input("Please enter the URL configuration to be deleted:"
118 " For example: www.baidu.com ")
119
120 delete(website)
121 elif op_haproxy == '4':
122 website = input("Please enter the URL configuration you want to modify:"
123 " For example: {'backend':'www.baidu.com','record': {'server': '100.1.7.8',"
124 " 'weight': 20,'maxconn': 3000}} ")
125 update(website)
126 elif op_haproxy == 'q':
127 break
128 else :
129 print("Please check your input!")
View Code
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.baidu.org
29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
haproxy配置文件
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.baidu.org
29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
1 #!/usr/bin/env python
2 #=*- coding:UTF-8 -*-
3
4 #查找
5 def search(website ):
6 reault_list = []#存放查询结果
7 search_flag = False #定义查询结果标志
8
9 with open(‘haproxy‘,‘r‘,encoding= ‘utf-8‘) as f:
10 for line in f:#if语句顺序进行,当前条件判断完,进入下一判断
11 if ‘backend {}‘.format(website) == line.strip():#找到backend www.oldboy.org 开始截取
12 search_flag = True
13 if line.strip().startswith(‘backend‘) and ‘backend {}‘.format(website) != line.strip() :#找到下一个 backend www.xxx.com部分结束,截取其中部分
14 search_flag = False
15 if search_flag:
16 reault_list.append(line .strip() )
17 print(reault_list )
18 f.close()
19 if reault_list == []:
20 print(‘对不起,您查询的网址不存在!‘)
21 else:
22 return reault_list
23
24 #增加
25 def add(website):
26 arg = eval(website) #将输入的字符串转变为字典
27 backend_title = ‘backend {}‘.format(arg[‘backend‘])# 要插入backend的字段
28 record_title = arg[‘record‘]
29 context_record = ‘server {0}{0} weight {1} maxconn {2}‘. 30 format(record_title[‘server‘],record_title[‘weight‘],record_title[‘maxconn‘] )
31 add_flag = True#设置新增标志位
32 with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as f:
33 for line in f:
34 if line.strip() == backend_title:
35 print("您新增的网址已经存在!")
36 add_flag = False
37 if add_flag:
38 f.write(‘ {}‘.format(backend_title))
39 f.write(‘ {}‘.format(context_record))
40 f.close()
41
42 #删除
43 def delete(website):
44 delete_flag = False #设置删除标志位
45 delete_result = True
46 with open(‘haproxy‘, ‘r‘, encoding=‘utf-8‘) as f, 47 open(‘haproxy_bak‘, ‘w‘) as f1:
48 for line in f:
49 if ‘backend {}‘.format(website ) == line.strip():
50 delete_flag = True
51 delete_result = False
52 continue
53 if line.strip().startswith(‘backend‘) and line.strip() != website:
54 delete_flag = False
55 if not delete_flag:
56 f1.write(line)
57 if delete_result :
58 print("您删除的网址不存在!")
59 with open(‘haproxy‘, ‘w‘) as f, 60 open(‘haproxy_bak‘, ‘r‘, encoding=‘utf-8‘) as f1:
61 for line in f1:
62 f.write(line)
63
64 #修改
65 def update(website):
66 arg = eval(website )
67 # print(arg)
68 backend_title = "backend {}".format(arg[‘backend‘]) # 要插入backend整个字段
69 # print(backend_title)
70 record_title = arg["record"]
71 context_record = "server {0} {0} weight {1} maxconn {2}". 72 format(record_title[‘server‘], record_title[‘weight‘], record_title[‘maxconn‘])
73 # print(context_record)
74
75 update_flag = False #设置修改标志位
76 update_re = False #设置重复修改位
77
78 with open(‘haproxy‘, ‘r‘, encoding=‘utf-8‘) as f, 79 open(‘haproxy_bak‘, ‘w‘) as f1:
80 for line in f:
81 if line.strip() == backend_title:
82 update_flag = True
83 continue #如果出现修改backend_title,设置修改标志位为True。
84 if line.strip().startswith(‘backend‘) and line.strip() != backend_title :
85 update_flag = False #捕捉到下一个backend_title,设置修改标志位为False。
86 if not update_flag:
87 f1.write(line)
88 if update_flag and not update_re: #修改标志位为True,并且没有被修改过
89 f1.write(‘ {}‘.format(backend_title))
90 f1.write(‘ {} ‘.format(context_record))
91 update_re = True
92
93 with open(‘haproxy‘, ‘w‘) as f, 94 open(‘haproxy_bak‘, ‘r‘, encoding=‘utf-8‘) as f1:
95 for line in f1:
96 f.write(line)
97
98
99
100
101 while True:
102 print(‘欢迎进入haproxy配置程序‘.center(50,‘-‘) )
103 print( ‘ 1 查询 ‘)
104 print(‘ 2 新增 ‘)
105 print(‘ 3 删除 ‘)
106 op_haproxy = input(‘选择要进入模式的ID-》:‘)
107 if op_haproxy == ‘1‘:
108 website = input(‘请输入要查询的网址: ‘
109 ‘例如www.oldboy.org‘)
110 search(website )
111 elif op_haproxy == ‘2‘:
112 website = input("请输入要新增的网址配置:"
113 "例如:{‘backend‘: ‘www.baidu.com‘,‘record‘: {‘server‘: ‘100.1.7.8‘,"
114 "‘weight‘: 20,‘maxconn‘: 3000}} ")
115 add(website)
116 elif op_haproxy == ‘3‘:
117 website = input("请输入要删除的网址配置:"
118 "例如:www.baidu.com ")
119
120 delete(website)
121 elif op_haproxy == ‘4‘:
122 website = input("请输入要修改的网址配置:"
123 "例如:{‘backend‘: ‘www.baidu.com‘,‘record‘: {‘server‘: ‘100.1.7.8‘,"
124 "‘weight‘: 20,‘maxconn‘: 3000}} ")
125 update(website)
126 elif op_haproxy == ‘q‘:
127 break
128 else:
129 print("请检查您的输入!")
View Code
1 #!/usr/bin/env python
2 #=*- coding:UTF-8 -*-
3
4 #查找
5 def search(website ):
6 reault_list = []#存放查询结果
7 search_flag = False #定义查询结果标志
8
9 with open(‘haproxy‘,‘r‘,encoding= ‘utf-8‘) as f:
10 for line in f:#if语句顺序进行,当前条件判断完,进入下一判断
11 if ‘backend {}‘.format(website) == line.strip():#找到backend www.oldboy.org 开始截取
12 search_flag = True
13 if line.strip().startswith(‘backend‘) and ‘backend {}‘.format(website) != line.strip() :#找到下一个 backend www.xxx.com部分结束,截取其中部分
14 search_flag = False
15 if search_flag:
16 reault_list.append(line .strip() )
17 print(reault_list )
18 f.close()
19 if reault_list == []:
20 print(‘对不起,您查询的网址不存在!‘)
21 else:
22 return reault_list
23
24 #增加
25 def add(website):
26 arg = eval(website) #将输入的字符串转变为字典
27 backend_title = ‘backend {}‘.format(arg[‘backend‘])# 要插入backend的字段
28 record_title = arg[‘record‘]
29 context_record = ‘server {0}{0} weight {1} maxconn {2}‘. 30 format(record_title[‘server‘],record_title[‘weight‘],record_title[‘maxconn‘] )
31 add_flag = True#设置新增标志位
32 with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as f:
33 for line in f:
34 if line.strip() == backend_title:
35 print("您新增的网址已经存在!")
36 add_flag = False
37 if add_flag:
38 f.write(‘ {}‘.format(backend_title))
39 f.write(‘ {}‘.format(context_record))
40 f.close()
41
42 #删除
43 def delete(website):
44 delete_flag = False #设置删除标志位
45 delete_result = True
46 with open(‘haproxy‘, ‘r‘, encoding=‘utf-8‘) as f, 47 open(‘haproxy_bak‘, ‘w‘) as f1:
48 for line in f:
49 if ‘backend {}‘.format(website ) == line.strip():
50 delete_flag = True
51 delete_result = False
52 continue
53 if line.strip().startswith(‘backend‘) and line.strip() != website:
54 delete_flag = False
55 if not delete_flag:
56 f1.write(line)
57 if delete_result :
58 print("您删除的网址不存在!")
59 with open(‘haproxy‘, ‘w‘) as f, 60 open(‘haproxy_bak‘, ‘r‘, encoding=‘utf-8‘) as f1:
61 for line in f1:
62 f.write(line)
63
64 #修改
65 def update(website):
66 arg = eval(website )
67 # print(arg)
68 backend_title = "backend {}".format(arg[‘backend‘]) # 要插入backend整个字段
69 # print(backend_title)
70 record_title = arg["record"]
71 context_record = "server {0} {0} weight {1} maxconn {2}". 72 format(record_title[‘server‘], record_title[‘weight‘], record_title[‘maxconn‘])
73 # print(context_record)
74
75 update_flag = False #设置修改标志位
76 update_re = False #设置重复修改位
77
78 with open(‘haproxy‘, ‘r‘, encoding=‘utf-8‘) as f, 79 open(‘haproxy_bak‘, ‘w‘) as f1:
80 for line in f:
81 if line.strip() == backend_title:
82 update_flag = True
83 continue #如果出现修改backend_title,设置修改标志位为True。
84 if line.strip().startswith(‘backend‘) and line.strip() != backend_title :
85 update_flag = False #捕捉到下一个backend_title,设置修改标志位为False。
86 if not update_flag:
87 f1.write(line)
88 if update_flag and not update_re: #修改标志位为True,并且没有被修改过
89 f1.write(‘ {}‘.format(backend_title))
90 f1.write(‘ {} ‘.format(context_record))
91 update_re = True
92
93 with open(‘haproxy‘, ‘w‘) as f, 94 open(‘haproxy_bak‘, ‘r‘, encoding=‘utf-8‘) as f1:
95 for line in f1:
96 f.write(line)
97
98
99
100
101 while True:
102 print(‘欢迎进入haproxy配置程序‘.center(50,‘-‘) )
103 print( ‘ 1 查询 ‘)
104 print(‘ 2 新增 ‘)
105 print(‘ 3 删除 ‘)
106 op_haproxy = input(‘选择要进入模式的ID-》:‘)
107 if op_haproxy == ‘1‘:
108 website = input(‘请输入要查询的网址: ‘
109 ‘例如www.oldboy.org‘)
110 search(website )
111 elif op_haproxy == ‘2‘:
112 website = input("请输入要新增的网址配置:"
113 "例如:{‘backend‘: ‘www.baidu.com‘,‘record‘: {‘server‘: ‘100.1.7.8‘,"
114 "‘weight‘: 20,‘maxconn‘: 3000}} ")
115 add(website)
116 elif op_haproxy == ‘3‘:
117 website = input("请输入要删除的网址配置:"
118 "例如:www.baidu.com ")
119
120 delete(website)
121 elif op_haproxy == ‘4‘:
122 website = input("请输入要修改的网址配置:"
123 "例如:{‘backend‘: ‘www.baidu.com‘,‘record‘: {‘server‘: ‘100.1.7.8‘,"
124 "‘weight‘: 20,‘maxconn‘: 3000}} ")
125 update(website)
126 elif op_haproxy == ‘q‘:
127 break
128 else:
129 print("请检查您的输入!")