代码需求:
-
- 可查询haproxy配置文件
- 可增加相关网址及记录
- 可删除相关网址及记录
- 可修改相关网址及记录
haproxy文件:
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.8 100.1.7.8 weight 20 maxconn 3000
backend www.sina.com
server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
haproxy
代码展示:
功能实现:
1、输入相关网址,可查询相关记录信息;
2、按照字典样式输入网址及记录信息,可在haproxy文件中增加;
3、输入相关网址,可删除相关记录信息;
4、按照字典样式输入网址及记录信息,可在haproxy文件中更新。
程序目录:
程序代码:
第一版代码:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 # 查询 6 def search(website): 7 result_list = [] #存放查询的结果 8 search_flag = False #定义查询结果标志 9 with open('haproxy','r',encoding='utf-8') as f: 10 for line in f: 11 # if 语句顺序进行,当前条件判断完,进入下一判断 12 if 'backend {}'.format(website) == line.strip(): #找到backend www.oldboy.org 开始截取 13 search_flag = True 14 if line.strip().startswith('backend') and line.strip() != 'backend {}'.format(website): 15 #找到下一个 backend www.xxx.com部分结束,截取其中部分 16 search_flag = False 17 if search_flag: 18 result_list.append(line.strip()) 19 print(result_list) 20 f.close() 21 if result_list == []: 22 print("对不起,您查询的网址不存在!") 23 else: 24 return result_list 25 26 # 新增 27 def add(website): 28 arg = eval(website) #将输入的字符串转变为字典 29 # print(arg) 30 backend_title = "backend {}".format(arg['backend']) # 要插入backend的字段 31 # print(backend_title) 32 record_title = arg["record"] 33 context_record = "server {0} {0} weight {1} maxconn {2}".\ 34 format(record_title['server'],record_title['weight'],record_title['maxconn']) 35 # print(context_record) 36 37 add_flag = True #设置新增标志位 38 with open('haproxy', 'r+', encoding='utf-8') as f: 39 for line in f: 40 if line.strip() == backend_title: 41 print("您新增的网址已经存在!") 42 add_flag = False 43 if add_flag: 44 f.write('\n{}'.format(backend_title)) 45 f.write('\n\t\t{}'.format(context_record)) 46 f.close() 47 48 # 删除 49 def delete(website): 50 51 delete_flag = False #设置删除标志位 52 with open('haproxy', 'r', encoding='utf-8') as f,\ 53 open('haproxy_bak','w') as f1 : 54 for line in f: 55 if 'backend {}'.format(website) == line.strip(): 56 delete_flag = True 57 continue #如果出现修改backend_title,设置修改标志位为True。 58 if line.strip().startswith('backend') and line.strip() != website : 59 delete_flag = False #捕捉到下一个backend_title,设置修改标志位为False。 60 if not delete_flag: 61 f1.write(line) 62 63 # if line.strip() != backend_title and line.strip() != context_record: 64 # f1.write(line) #如果文件内容行不等于要删除内容,写入备份文件中 65 66 if delete_flag == False: 67 print("您删除的网址不存在!") 68 69 with open('haproxy', 'w') as f,\ 70 open('haproxy_bak','r',encoding='utf-8') as f1 : #将备份文件写入原文件中 71 for line in f1: 72 f.write(line) 73 #修改 74 def update(website): 75 arg = eval(website) 76 # print(arg) 77 backend_title = "backend {}".format(arg['backend']) # 要插入backend整个字段 78 # print(backend_title) 79 record_title = arg["record"] 80 context_record = "server {0} {0} weight {1} maxconn {2}". \ 81 format(record_title['server'], record_title['weight'], record_title['maxconn']) 82 # print(context_record) 83 84 update_flag = False #设置修改标志位 85 update_re = False #设置重复修改位 86 87 with open('haproxy', 'r', encoding='utf-8') as f, \ 88 open('haproxy_bak', 'w') as f1: 89 for line in f: 90 if line.strip() == backend_title: 91 update_flag = True 92 continue #如果出现修改backend_title,设置修改标志位为True。 93 if line.strip().startswith('backend') and line.strip() != backend_title : 94 update_flag = False #捕捉到下一个backend_title,设置修改标志位为False。 95 if not update_flag: 96 f1.write(line) 97 if update_flag and not update_re: #修改标志位为True,并且没有被修改过 98 f1.write('\n{}'.format(backend_title)) 99 f1.write('\n\t\t{}\n'.format(context_record)) 100 update_re = True 101 102 with open('haproxy', 'w') as f, \ 103 open('haproxy_bak', 'r', encoding='utf-8') as f1: 104 for line in f1: 105 f.write(line) 106 107 108 #主函数 109 while True: 110 print("欢迎进入haproxy配置程序".center(50, "#"), 111 "\n1 查询\n" 112 "2 新增\n" 113 "3 删除\n" 114 "4 修改\n" 115 "q 退出程序\n") 116 op_haproxy = input("选择要进入模式的ID:") 117 if op_haproxy == '1': 118 website = input("请输入要查询的网址:" 119 "例如:www.oldboy.org\n") 120 search(website) 121 elif op_haproxy == '2': 122 website = input("请输入要新增的网址配置:" 123 "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8'," 124 "'weight': 20,'maxconn': 3000}}\n") 125 add(website) 126 elif op_haproxy == '3': 127 website = input("请输入要删除的网址配置:" 128 "例如:www.baidu.com\n") 129 delete(website) 130 elif op_haproxy =='4': 131 website = input("请输入要修改的网址配置:" 132 "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8'," 133 "'weight': 20,'maxconn': 3000}}\n") 134 update(website) 135 elif op_haproxy == 'q': 136 break 137 else: 138 print("请检查您的输入!")
执行效果:
1 E:\Python\PythonExercising\haproxy\venv\Scripts\python.exe E:/Python/PythonExercising/haproxy/haproxy.py 2 #################欢迎进入haproxy配置程序################## 3 1 查询 4 2 新增 5 3 删除 6 4 修改 7 q 退出程序 8 9 选择要进入模式的ID:1 10 请输入要查询的网址:例如:www.oldboy.org 11 www.oldboy.org 12 ['backend www.oldboy.org', 'server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000'] 13 #################欢迎进入haproxy配置程序################## 14 1 查询 15 2 新增 16 3 删除 17 4 修改 18 q 退出程序 19 20 选择要进入模式的ID:2 21 请输入要新增的网址配置:例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 22 {'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 23 #################欢迎进入haproxy配置程序################## 24 1 查询 25 2 新增 26 3 删除 27 4 修改 28 q 退出程序 29 30 选择要进入模式的ID:3 31 请输入要删除的网址配置:例如:www.baidu.com 32 www.baidu.com 33 #################欢迎进入haproxy配置程序################## 34 1 查询 35 2 新增 36 3 删除 37 4 修改 38 q 退出程序 39 40 选择要进入模式的ID:4 41 请输入要修改的网址配置:例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 42 {'backend': 'www.oldboy.org','record': {'server': '100.1.7.5','weight': 20,'maxconn': 3000}} 43 #################欢迎进入haproxy配置程序################## 44 1 查询 45 2 新增 46 3 删除 47 4 修改 48 q 退出程序 49 50 选择要进入模式的ID:q 51 52 Process finished with exit code 0
新增后配置文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.8 100.1.7.8 weight 20 maxconn 3000 backend www.sina.com server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000 backend www.baidu.com server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000
删除后配置文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.8 100.1.7.8 weight 20 maxconn 3000 backend www.sina.com server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
修改后配置文件:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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.5 100.1.7.5 weight 20 maxconn 3000 backend www.sina.com server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000