我是靠谱客的博主 聪慧花瓣,最近开发中收集的这篇文章主要介绍haproxy.conf文件操作(基于函数方式),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

def frend_msg():
    frendmsg_dict = {}
    prefix_opt = {"select":"查询","add":"添加","modify":"修改","delete":"删除"}
    frendmsg_dict["frend"] ="33[33;1m亲!!你当前输入的域名不存在请重新输入哈 33[0m"
    for i, v in enumerate(prefix_opt.items()):
        v_info = list(v)
        frendmsg_dict[v_info[0]] = "33[33;1m亲!!恭喜您" + v_info[1] + "成功了33[0m"
    # frendmsg_dict["modify"] = "33[33;1m亲!!恭喜您修改成功了33[0m"
    frendmsg_dict["sucess"] = "33[33;1m亲!!恭喜您操作成功33[0m"
    return frendmsg_dict

def message(*args):
    ip_input  =  input("请输入ip").strip()
    weight_input  =  input("请输入权重").strip()
    max_conn_input  =  input("请输入最大连接:只能数字").strip()
    return "ttserver %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("33[33;1m亲!!请您按选项输入要修改的内容33[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 

 

转载于:https://www.cnblogs.com/hongyongshang/p/6706911.html

最后

以上就是聪慧花瓣为你收集整理的haproxy.conf文件操作(基于函数方式)的全部内容,希望文章能够帮你解决haproxy.conf文件操作(基于函数方式)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部