程序:修改haproxy配置文件
需求:
1、查 输入:www.oldboy.org 获取当前backend下的所有记录 2、新建 输入: arg = { ‘bakend‘: ‘www.oldboy.org‘, ‘record‘:{ ‘server‘: ‘100.1.7.9‘, ‘weight‘: 20, ‘maxconn‘: 30 } } 3、删除 输入: arg = { ‘bakend‘: ‘www.oldboy.org‘, ‘record‘:{ ‘server‘: ‘100.1.7.9‘, ‘weight‘: 20, ‘maxconn‘: 30 } }
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.9 100.1.7.9 weight 20 maxconn 3000
作业代码:
# Author:soap menu = { ‘search‘, ‘add‘, ‘delete‘, ‘exit‘ } exit_flag = 1 while exit_flag: for k in menu: print (k) chioce = input(‘>>:‘) if chioce == ‘search‘: with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as file: search_info = ‘backend ‘ + input(‘please input the web site: ‘) for line in file: if search_info in line: print(file.readline()) elif chioce == ‘add‘: with open(‘haproxy‘, ‘a+‘, encoding=‘utf-8‘) as file: add_info = eval(input(‘plaese input the web site info: ‘)) write_info_1 = ‘backend ‘ + str(add_info[‘backend‘]) + ‘\n‘ write_info_2 = ‘ ‘ + ‘server ‘ + str(add_info[‘record‘][‘server‘]) + ‘ weight ‘ + str(add_info[‘record‘][‘weight‘]) + ‘ maxconn ‘ + str(add_info[‘record‘][‘maxconn‘]) + ‘\n‘ file.write(write_info_1) file.write(write_info_2) elif chioce == ‘delete‘: with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as file: delete_info = eval(input(‘plaese input the web site info: ‘)) find_str_1 = ‘backend ‘ + delete_info[‘backend‘] find_str_2 = ‘server ‘ + delete_info[‘record‘][‘server‘] for line in file: if find_str_1 in line: continue if find_str_2 in line: continue file.write(line) elif chioce == ‘exit‘: exit_flag = 0