首页 > 其他 > 详细

练习:修改haproxy文件配置

时间:2020-09-12 22:58:16      阅读:70      评论:0      收藏:0      [点我收藏+]

要求:

# 修改haproxy配置文件
# 查询:输入网址获取当前backend下的所有记录
# 增加:输入字典增加一条记录
# 删除:输入字典删除该记录
目前完成的版本有一个比较大的问题就是增加删除的时候如果不按格式输入代码会报错,后续再改进吧

技术分享图片
 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.oldboy.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
haproxy文档
技术分享图片
  1 # 修改haproxy配置文件
  2 # 查询:输入网址获取当前backend下的所有记录
  3 # 增加:输入字典增加一条记录
  4 # 删除:输入字典删除该记录
  5 
  6 import sys
  7 
  8 
  9 def info():
 10     """提取当前配置文件中所有backend网址信息"""
 11     backend = []
 12     with open("haproxy", r) as f:
 13         for line in f:
 14             if line.startswith(backend):
 15                 line = line.strip()
 16                 backend.append(line.partition( )[2])
 17     return backend
 18 
 19 
 20 def search(website):
 21     """输入网址查找其具体信息"""
 22     info_backend = []
 23     if website in info():
 24         with open(haproxy, r) as f:
 25             for line in f:
 26                 if line.strip() == backend+ +website:
 27                     f1 = f.readline()
 28                     while f1.startswith(        server):
 29                         info_backend.append(f1.strip())
 30                         f1 = f.readline()
 31             print(f"\033[31m{website}条目信息:\033[0m")
 32             for i, items in enumerate(info_backend):
 33                 print(i+1, items.strip(""))
 34     else:
 35         print(f"{website} 条目不存在")
 36     return info_backend
 37 
 38 
 39 def add(add_dic):
 40     """添加条目"""
 41     add_dic = eval(add_dic)
 42     website = add_dic[backend]
 43     record = add_dic[record]
 44     if website in info():
 45         print(f\033[31m{website}已存在\033[0m)
 46     else:
 47         with open(haproxy, a) as f:
 48             f.write(\n\n+backend+ +website+\n)
 49             f.write(        +server+ +record[server]+ +record[server]+ )
 50             f.write(weight+ +str(record[weight])+ )
 51             f.write(maxconn+ +str(record[maxconn]))
 52             print(f"\033[31mbackend {website} 条目已添加\033[0m")
 53 
 54 
 55 def delete(del_dic):
 56     """删除条目"""
 57     del_dic = eval(del_dic)
 58     website = del_dic[backend]
 59     if website in info():
 60         with open(haproxy, r) as f:
 61             with open(haproxy_new.txt, w+) as f1:
 62                 for line in f:
 63                     if line.strip() == backend+ +del_dic[backend]:
 64                         line = f.readline()
 65                         while line.startswith(        server):
 66                             line = f.readline()
 67                             continue
 68                     else:
 69                         f1.write(line)
 70         print(f"{website} 条目删除成功")
 71         with open(haproxy, w) as f:
 72             with open(haproxy_new.txt, r) as f1:
 73                 for line in f1:
 74                     f.write(line)
 75     else:
 76         print(f"{website} 条目不存在")
 77     return
 78 
 79 
 80 print("\033[31m欢迎进入haproxy配置系统\033[0m".center(50,  ))
 81 while True:
 82     print(-.center(50, -))
 83     print("当前backend网址信息如下:")
 84     for j, backend in enumerate(info()):
 85         print(j+1, backend)
 86     print(-.center(50, -))
 87     print("1.信息查询", \n, "2.增加条目", \n, "3.删除条目", sep=‘‘)
 88     choice1 = input("输入序号进入配置界面(输入q退出系统):")
 89     if choice1.isdigit():
 90         choice1 = int(choice1)
 91         if choice1 in range(1, 4):
 92             if choice1 == 1:
 93                 while True:
 94                     choice2 = input("请输入网址进行查询(例:www.baidu.com)(b返回q退出):")
 95                     if choice2 == b:
 96                         break
 97                     elif choice2 == q:
 98                         print("系统退出")
 99                         sys.exit()
100                     else:
101                         search(choice2)
102             elif choice1 == 2:
103                 while True:
104                     choice3 = input("请输入条目字典(b返回q退出):")
105                     if choice3 == b:
106                         break
107                     elif choice3 == q:
108                         print("系统退出")
109                         sys.exit()
110                     else:
111                         add(choice3)
112             elif choice1 == 3:
113                 while True:
114                     choice4 = input("请输入添加条目字典(b返回q退出):")
115                     if choice4 == b:
116                         break
117                     elif choice4 == q:
118                         print("系统退出")
119                         sys.exit()
120                     else:
121                         delete(choice4)
122         else:
123             print("无此选项,请正确输入!")
124     elif choice1 == q:
125         print("系统退出")
126         sys.exit()
127     else:
128         print("格式错误,请重新输入!")
129         continue
haproxy代码

练习:修改haproxy文件配置

原文:https://www.cnblogs.com/bitsjc/p/13656827.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!