首页 > 代码库 > haproxy.conf文件操作(基于函数方式)
haproxy.conf文件操作(基于函数方式)
def frend_msg(): frendmsg_dict = {} prefix_opt = {"select":"查询","add":"添加","modify":"修改","delete":"删除"} frendmsg_dict["frend"] ="\033[33;1m亲!!你当前输入的域名不存在请重新输入哈 \033[0m" for i, v in enumerate(prefix_opt.items()): v_info = list(v) frendmsg_dict[v_info[0]] = "\033[33;1m亲!!恭喜您" + v_info[1] + "成功了\033[0m" # frendmsg_dict["modify"] = "\033[33;1m亲!!恭喜您修改成功了\033[0m" frendmsg_dict["sucess"] = "\033[33;1m亲!!恭喜您操作成功\033[0m" return frendmsg_dict def message(*args): ip_input = input("请输入ip").strip() weight_input = input("请输入权重").strip() max_conn_input = input("请输入最大连接:只能数字").strip() return "\t\tserver %s weight %s maxconn %s \n" %(ip_input,weight_input,max_conn_input) def select_file(file,info_input): with open(file,encoding="utf8") as f_read: flag = False file_lst = [] for line in f_read : if line.startswith("backend") and str(info_input) in line: flag = True continue elif line.startswith("backend") and flag: break; if flag: file_lst.append(line) f_read.close() return file_lst #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ def add_file(): print("add") info_input = input("请输入域名 ") with open(src_file, "r+", encoding="utf8") as f_read, open(dst_file, "w+", encoding="utf8") as f_write: flag = False file_lst = [] do_mess = message() for line in f_read: f_write.write(line) f_write.flush() if line.startswith("backend") and str(info_input) in line: file_lst.append(do_mess) f_write.write("\n".join(file_lst)) f_write.flush() print(frend_msg()["add"]) flag = True continue if flag: file_lst.append(line) savefile() #___________________________________________________________________ def modify_file(): print("modify") info_input = input("请输入域名 ") with open(src_file, "r+", encoding="utf8") as f_read, open(dst_file, "w", encoding="utf8") as f_write: flag = False do_mess = message() print("\033[33;1m亲!!请您按选项输入要修改的内容\033[0m") rep_change = message() for line in f_read: if line.startswith("backend") and str(info_input) in line.strip(): # print("----", line) f_write.write(line) flag = True continue if len(line) == line.count("\n"): if len(line) == 0: oldline = line.strip() newline = line.replace(oldline, rep_change.strip()) f_write.write(newline) continue if flag: if line.strip() == do_mess.strip(): oldline = line.strip() newline = line.replace(oldline,rep_change.strip()) f_write.write(newline) continue # file_lst.append(do_mess) # print("???",line) f_write.write(line) # print("fff", line) print(frend_msg()["modify"]) savefile() #_________________________________________________________________ def delete_file(): print("delete") info_input = input("请输入域名 ") with open(src_file, "r+", encoding="utf8") as f_read, open(dst_file, "w", encoding="utf8") as f_write: flag = False do_mess = message() for line in f_read: if line.startswith("backend") and str(info_input) in line.strip(): # print("----", line) f_write.write(line) flag = True continue if len(line) == line.count("\n"): continue if flag: if line.strip() == do_mess.strip(): continue f_write.write(line) # print("fff", line) print(frend_msg()["delete"]) savefile() ################################################################### def savefile(): from os import rename, remove remove(src_file) rename(dst_file, src_file) ans = input(">>>") #--------------------------------------------------------------------- #--------------------------------------------------------------------- src_file = "haproxy.conf" dst_file = "modify.conf" def main(): quary_flag = False while not quary_flag: # with open(src_file, "r+", encoding="utf8") as f_read: select = ["查询","修改","添加","删除","退出"] for opt in select: print (select.index(opt),opt) user_input = input("please intput num") if user_input.isdigit() and int(user_input)>=0 and int(user_input) < len(select): user_input = int(user_input) indx_opt = select[user_input] opt_input= input(indx_opt+">>>") if opt_input == "0": info_input = input("请输入域名>>> ") for x in select_file(src_file,info_input): print(x.strip()) elif opt_input == "1": modify_file() elif opt_input == "2": add_file() elif opt_input == "3": delete_file() elif opt_input == "4": quary_flag = True; break if quary_flag: break else: continue # print(opt_input) elif user_input == "q": continue elif user_input == "Q": break else: continue if __name__ == "__main__": main()
haproyx.conf文件
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.oldboy1.org server 10.2.3.1 weight 1 maxconn 10 server 1.3.3.3 weight 1 maxconn 1 server 10.3.3.2 weight 12 maxconn 100 server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000 backend www.oldboy2.org server 10.2.2.1 weight 1 maxconn 1 server 1.2.2.1 weight 10 maxconn 20 server 10.3.2.22 weight 10 maxconn 1000 server 10.3.2.1 weight 2 maxconn 10 server 192.168.1.2 192.168.1.3 weight 2 maxconn 100 server 2.2.1.1 weight 10 maxconn 100 backend www.oldboy20.org server 192.168.168.21 weight 2 maxconn 600 server 10.8.2.10 weight 8 maxconn 1000 server 1.2.3.2 weight 1 maxconn 1 server 10.18.10.21 weight 1 maxconn 300
haproxy.conf文件操作(基于函数方式)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。