• 修改配置文件haproxy


       文件内容

     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 
    29 backend www.bigd.org                #节点bidg
    30     server10.1.7.9 weight 20 maxconn 3000
    31 
    32 backend www.qq.com                 #qq节点
    33     server171.8.7.7 weight 70 maxconn 3000

       代码部分

      1 #Author BigD
      2 # -*- coding:utf-8 -*-
      3 
      4 #修改配置文件haproxy
      5 # 1、查询节点下信息
      6 # 2、新建节点
      7 # 3、删除节点
      8 # 4、修改节点
      9 
     10 ###################################读取文件################################
     11 current_index = -1
     12 list = []
     13 dict_all = {}
     14 old_content = []
     15 print("有如下节点:")
     16 for index,line in enumerate(open("haproxy","r")):
     17 
     18     if line.startswith("backend"):
     19         current_index = index + 1
     20         nod = line.strip().split()[1]
     21 
     22         dict_all[nod.split(".")[1]]={}
     23         dict_all[nod.split(".")[1]]["bakend"] = nod
     24     elif index == current_index:
     25         dict_all[nod.split(".")[1]]["record"] = {}
     26         dict_all[nod.split(".")[1]]["record"]["server"] = line.strip().split()[1]
     27         dict_all[nod.split(".")[1]]["record"]["weight"] = line.strip().split()[3]
     28         dict_all[nod.split(".")[1]]["record"]["maxconn"] = line.strip().split()[5]
     29     else:
     30         old_content.append(line.strip())
     31 open("haproxy","r").close()
     32 ##########################代码正文###############################################
     33 while True:
     34     print("请选择需要的功能".center(50,"="))
     35     print("已有的节点:")
     36     for dict_one in dict_all:
     37         print(dict_one)
     38 
     39     print("现有功能:
    1、查询节点;
    2、增加节点;
    3、修改节点;
    4、删除节点
    5、退出。")
     40     choice = input("请选择你需要的功能:")
     41     if choice == "1":         #查询
     42         while True:
     43             choice_query = input("请输入查询的节点,退出(q):")
     44             if choice_query == "q":
     45                 break
     46             if choice_query in dict_all:                                            #查询
     47                 print("节点名称:",dict_all[choice_query]["bakend"])
     48                 print("ip地址:",dict_all[choice_query]["record"]["server"])
     49                 print("权重:",dict_all[choice_query]["record"]["weight"])
     50                 print("连接数:",dict_all[choice_query]["record"]["maxconn"])
     51             else:
     52                 print("wrong!")
     53 
     54     elif choice == "2":         #增加
     55         # print("增加")
     56         while True:
     57             add_simple_name = input("请输出节点简称")
     58             add_name = input("请输出节点名称")
     59             add_ip = input("请输出节点ip")
     60             add_weight = input("请输出节点权重")
     61             add_maxconn = input("请输出节点最大连接数")
     62             dict_all[add_simple_name]={}
     63             dict_all[add_simple_name]["bakend"] = add_name                      #节点简称
     64             dict_all[add_simple_name]["record"] = {}
     65             dict_all[add_simple_name]["record"]["server"] = add_ip              #节点ip
     66             dict_all[add_simple_name]["record"]["weight"] = add_weight          #节点权重
     67             dict_all[add_simple_name]["record"]["maxconn"] = add_maxconn        #节点最大连接数
     68             print("添加成功",dict_all[add_simple_name])
     69             break
     70     elif choice == "3":
     71         # print("修改")
     72         while True:
     73             modify_nod = input("你想修改哪个节点啊?退出(q)")
     74             if modify_nod == "q":
     75                 break
     76             if modify_nod in dict_all:
     77                 modify_nod
     78                 dict_all[modify_nod]["bakend"] = input("请输出修改后节点名称")
     79                 dict_all[modify_nod]["record"]["server"] = input("请输出修改后节点ip")
     80                 dict_all[modify_nod]["record"]["weight"] = input("请输出修改后节点权重")
     81                 dict_all[modify_nod]["record"]["maxconn"] = input("请输出修改后节点最大连接数")
     82     elif choice == "4":
     83         # print("删除")
     84         while True:
     85             delete_nod = input("你想删除那个节点呢,退出(q)?")
     86             if delete_nod == "q":
     87                 break
     88             if delete_nod in dict_all:
     89                 dict_all.pop(delete_nod)    #删除1
     90                 # del dict_all[delete_nod]    #删除2
     91                 print("删除成功!")
     92             else:
     93                 print("没有这个节点,重新输入!")
     94 
     95     elif choice == "5":
     96         # print("退出")
     97         open("haproxy", "w").write("")
     98         for i in old_content:
     99             # print(i)
    100             open("haproxy", "a").write("%s
    "%i)
    101             # open("haproxy_new", "a").write("
    ")
    102             #
    103             # open("haproxy_new", "a").write(dict_all)
    104         for i in dict_all:
    105             print(i)
    106             open("haproxy", "a").write("backend %s
    	server%s weight %s maxconn %s
    
    "%(dict_all[i]["bakend"],dict_all[i]["record"]["server"],dict_all[i]["record"]["weight"],dict_all[i]["record"]["maxconn"]))
    107     else:
    108         pass

    现有功能:
    1、查询节点;
    2、增加节点;
    3、修改节点;
    4、删除节点
    5、退出。
    请选择你需要的功能:

  • 相关阅读:
    Windows中的库编程(三、函数调用约定 Calling Convention)
    weui
    js 压缩图片
    django 跨域访问
    html5
    有用的网站
    Chrome
    srpingBoot配置多环境配置文件
    Mysql在查询时不区分大小写
    [CentOS7]Nginx 1.20.1不支持四层负载
  • 原文地址:https://www.cnblogs.com/dabingya/p/6238306.html
Copyright © 2020-2023  润新知