python_haproxy



1
#!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import os,json 4 5 conf = "haproxy.cfg" 6 confnew = "haproxy.cfg.new" 7 def fetch(data): 8 backend_list = [] 9 backend_all_dict = {} 10 backend_dict = {} 11 server_list = [] 12 with open(conf, 'r', encoding='utf-8') as file: 13 for line in file: 14 line = line.strip() 15 # print(line) 16 if line.startswith('backend') == True: # 判断是否是backend开头,读取backend信息写到backend_list中 17 backend_name = line.split()[1] # 取出backend后的域名 18 backend_list.append(backend_name) # 将文件中backend加到列表中 19 20 elif line.startswith('server') == True: # 判断是否是server开头,读取server信息写到backend_all_dict中 21 backend_dict['name'] = line.split()[1] 22 backend_dict['IP'] = line.split()[2] 23 backend_dict['weight'] = line.split()[4] 24 backend_dict['maxconn'] = line.split()[6] 25 server_list.append(backend_dict.copy()) 26 backend_all_dict[backend_name] = server_list 27 for b in backend_list: 28 if b == data: 29 return backend_all_dict[b] 30 else: 31 continue 32 33 34 def add(data): 35 pass 36 37 38 39 40 41 def remove(data): 42 pass 43 44 45 46 47 48 49 if __name__ == '__main__': 50 msg=''' 51 1:查询 52 2:添加 53 3:删除 54 4: 退出 55 ''' 56 menu_dic={ 57 '1':fetch, 58 '2':add, 59 '3':remove, 60 '4':exit, 61 } 62 63 while True: 64 print(msg) 65 choice = input('操作>>:').strip() 66 if len(choice) == 0 or choice not in menu_dic:continue 67 if choice == 4: break 68 69 data = str(input('数据>>:').strip()) 70 #men_dic[choice]==fetch 71 print(menu_dic[choice](data)) 72 # menu_dic[choice](data)

 

 

配置文件

frontend www
bind *:80
acl web hdr_beg(host) 1.1.1.1
acl web1 hdr_beg(host) 2.2.2.2
use_backend webserver if web
use_backend web1server if web1

backend www.bruce.org
server 3.3.3.3 3.3.3.3 weight 20 maxcon 3000
server 4.4.4.4 4.4.4.4 weight 20 maxcon 3000

backend www.oldboy2.org
server 5.5.5.5 5.5.5.5 weight 20 maxcon 3000
server 6.6.6.6 6.6.6.6 weight 20 maxcon 3000
posted @ 2017-05-29 10:39  Bruce.yin  阅读(71)  评论(0编辑  收藏  举报