Python实践之路5——修改haproxy配置文件

代码需求:

    • 可查询haproxy配置文件
    • 可增加相关网址及记录
    • 可删除相关网址及记录
    • 可修改相关网址及记录

  haproxy文件:

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.8 100.1.7.8 weight 20 maxconn 3000
backend www.sina.com
server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000


haproxy

 代码展示:

    功能实现:

    1、输入相关网址,可查询相关记录信息;

    2、按照字典样式输入网址及记录信息,可在haproxy文件中增加;

    3、输入相关网址,可删除相关记录信息;

    4、按照字典样式输入网址及记录信息,可在haproxy文件中更新。

    程序目录:

    程序代码:

    第一版代码:

  1 #!/user/bin/env ptyhon
  2 # -*- coding:utf-8 -*-
  3 # Author: VisonWong
  4 
  5 # 查询
  6 def search(website):
  7     result_list = []  #存放查询的结果
  8     search_flag = False #定义查询结果标志
  9     with open('haproxy','r',encoding='utf-8') as f:
 10         for line in f:
 11             # if 语句顺序进行,当前条件判断完,进入下一判断
 12             if 'backend {}'.format(website) == line.strip():               #找到backend www.oldboy.org 开始截取
 13                search_flag = True
 14             if line.strip().startswith('backend') and line.strip() != 'backend {}'.format(website):
 15                                                                             #找到下一个 backend www.xxx.com部分结束,截取其中部分
 16                 search_flag = False
 17             if search_flag:
 18                 result_list.append(line.strip())
 19         print(result_list)
 20         f.close()
 21     if result_list == []:
 22         print("对不起,您查询的网址不存在!")
 23     else:
 24         return result_list
 25 
 26 # 新增
 27 def add(website):
 28     arg = eval(website)                  #将输入的字符串转变为字典
 29     # print(arg)
 30     backend_title = "backend {}".format(arg['backend'])  # 要插入backend的字段
 31     # print(backend_title)
 32     record_title = arg["record"]
 33     context_record = "server {0} {0} weight {1} maxconn {2}".\
 34         format(record_title['server'],record_title['weight'],record_title['maxconn'])
 35     # print(context_record)
 36 
 37     add_flag = True  #设置新增标志位
 38     with open('haproxy', 'r+', encoding='utf-8') as f:
 39         for line in f:
 40           if line.strip() == backend_title:
 41               print("您新增的网址已经存在!")
 42               add_flag = False
 43         if add_flag:
 44             f.write('\n{}'.format(backend_title))
 45             f.write('\n\t\t{}'.format(context_record))
 46         f.close()
 47 
 48 # 删除
 49 def delete(website):
 50 
 51     delete_flag = False  #设置删除标志位
 52     with open('haproxy', 'r', encoding='utf-8') as f,\
 53           open('haproxy_bak','w') as f1 :
 54         for line in f:
 55             if 'backend {}'.format(website) == line.strip():
 56                delete_flag = True
 57                continue                            #如果出现修改backend_title,设置修改标志位为True。
 58             if line.strip().startswith('backend') and line.strip() != website :
 59                 delete_flag = False                #捕捉到下一个backend_title,设置修改标志位为False。
 60             if not delete_flag:
 61                 f1.write(line)
 62 
 63             # if line.strip() != backend_title and line.strip() != context_record:
 64             #         f1.write(line)                            #如果文件内容行不等于要删除内容,写入备份文件中
 65 
 66         if delete_flag == False:
 67             print("您删除的网址不存在!")
 68 
 69     with open('haproxy', 'w') as f,\
 70           open('haproxy_bak','r',encoding='utf-8') as f1 :     #将备份文件写入原文件中
 71         for line in f1:
 72             f.write(line)
 73 #修改
 74 def update(website):
 75     arg = eval(website)
 76     #    print(arg)
 77     backend_title = "backend {}".format(arg['backend'])  # 要插入backend整个字段
 78     #    print(backend_title)
 79     record_title = arg["record"]
 80     context_record = "server {0} {0} weight {1} maxconn {2}". \
 81         format(record_title['server'], record_title['weight'], record_title['maxconn'])
 82     #    print(context_record)
 83 
 84     update_flag = False  #设置修改标志位
 85     update_re = False    #设置重复修改位
 86 
 87     with open('haproxy', 'r', encoding='utf-8') as f, \
 88             open('haproxy_bak', 'w') as f1:
 89         for line in f:
 90             if line.strip() == backend_title:
 91                update_flag = True
 92                continue                            #如果出现修改backend_title,设置修改标志位为True。
 93             if line.strip().startswith('backend') and line.strip() != backend_title :
 94                 update_flag = False                #捕捉到下一个backend_title,设置修改标志位为False。
 95             if not update_flag:
 96                 f1.write(line)
 97             if update_flag and  not update_re:    #修改标志位为True,并且没有被修改过
 98                 f1.write('\n{}'.format(backend_title))
 99                 f1.write('\n\t\t{}\n'.format(context_record))
100                 update_re = True
101 
102     with open('haproxy', 'w') as f, \
103             open('haproxy_bak', 'r', encoding='utf-8') as f1:
104         for line in f1:
105             f.write(line)
106 
107 
108 #主函数
109 while True:
110     print("欢迎进入haproxy配置程序".center(50, "#"),
111           "\n1 查询\n"
112           "2 新增\n"
113           "3 删除\n"
114           "4 修改\n"
115           "q 退出程序\n")
116     op_haproxy = input("选择要进入模式的ID:")
117     if op_haproxy == '1':
118         website = input("请输入要查询的网址:"
119                         "例如:www.oldboy.org\n")
120         search(website)
121     elif op_haproxy == '2':
122         website = input("请输入要新增的网址配置:"
123                         "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8',"
124                "'weight': 20,'maxconn': 3000}}\n")
125         add(website)
126     elif op_haproxy == '3':
127         website = input("请输入要删除的网址配置:"
128                         "例如:www.baidu.com\n")
129         delete(website)
130     elif op_haproxy =='4':
131         website = input("请输入要修改的网址配置:"
132                         "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8',"
133                "'weight': 20,'maxconn': 3000}}\n")
134         update(website)
135     elif op_haproxy == 'q':
136         break
137     else:
138         print("请检查您的输入!")

    执行效果:

 1 E:\Python\PythonExercising\haproxy\venv\Scripts\python.exe E:/Python/PythonExercising/haproxy/haproxy.py
 2 #################欢迎进入haproxy配置程序################## 
 3 1 查询
 4 2 新增
 5 3 删除
 6 4 修改
 7 q 退出程序
 8 
 9 选择要进入模式的ID:1
10 请输入要查询的网址:例如:www.oldboy.org
11 www.oldboy.org
12 ['backend www.oldboy.org', 'server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000']
13 #################欢迎进入haproxy配置程序################## 
14 1 查询
15 2 新增
16 3 删除
17 4 修改
18 q 退出程序
19 
20 选择要进入模式的ID:2
21 请输入要新增的网址配置:例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}}
22 {'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}}
23 #################欢迎进入haproxy配置程序################## 
24 1 查询
25 2 新增
26 3 删除
27 4 修改
28 q 退出程序
29 
30 选择要进入模式的ID:3
31 请输入要删除的网址配置:例如:www.baidu.com
32 www.baidu.com
33 #################欢迎进入haproxy配置程序################## 
34 1 查询
35 2 新增
36 3 删除
37 4 修改
38 q 退出程序
39 
40 选择要进入模式的ID:4
41 请输入要修改的网址配置:例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}}
42 {'backend': 'www.oldboy.org','record': {'server': '100.1.7.5','weight': 20,'maxconn': 3000}}
43 #################欢迎进入haproxy配置程序################## 
44 1 查询
45 2 新增
46 3 删除
47 4 修改
48 q 退出程序
49 
50 选择要进入模式的ID:q
51 
52 Process finished with exit code 0

  新增后配置文件:

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.8 100.1.7.8 weight 20 maxconn 3000
backend www.sina.com
        server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
backend www.baidu.com
    server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000
View Code

  删除后配置文件:

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.8 100.1.7.8 weight 20 maxconn 3000
backend www.sina.com
        server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
View Code

  修改后配置文件:

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.5 100.1.7.5 weight 20 maxconn 3000
backend www.sina.com
        server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
View Code

   第二版代码:

 1 #!/user/bin/env ptyhon
 2 # -*- coding:utf-8 -*-
 3 # Author: VisonWong
 4 
 5 def if_continue():  # 定义函数if_continue() 提示用户是否继续操作
 6     if_cont = input("\n\33[34;1mDo you want to continue to operate on files【y】/【n】:\33[0m\n")
 7     if if_cont == "y":
 8         pass
 9     else:
10         exit()
11 
12 
13 def info_message(options):  # 定义函数info_message() 提示用户操作信息
14     print("\33[31;1mInfo of %s\33[0m".center(50, "-") % options)
15 
16 
17 with open("haproxy", "a+", encoding="utf-8") as file_haproxy:  # a+模式打开haproxy文件
18     while True:  # 设置while循环
19         dict_file = {}
20         file_haproxy.seek(0)  # 移动光标到文件首部
21         for line in file_haproxy:
22             if "backend" in line and "use_backend" not in line:  # 提前文件中backend信息并生成字典dict_file
23                 dict_file[line.split()[1]] = file_haproxy.readline().strip()
24 
25         print("File_Operations_Backend".center(50, "*"), "\n1\tQuery\n2\tAdd\n3\tDel\nq\tQuit")
26         user_choice = input("\33[34;1mSelect the ID to operate:\33[0m")  # 让用户选择操作文件的模式
27 
28         if user_choice == "1":
29             info_query = input("\33[34;1mInput information to query:\33[0m")
30             if info_query in dict_file.keys():  # 判断输入的查询的信息是否存在
31                 info_message("Query")
32                 print(dict_file[info_query])  # 如果查询的backend存在 打印查询的信息
33             else:  # 否则提示没有查询到相关信息
34                 print("\33[31;1mError:No query to the corresponding information!\33[0m")
35             if_continue()
36 
37         elif user_choice == "2":
38             info_add = input("\33[34;1mInput information to add:\33[0m")
39             try:  # 判断输入的类型是否可以转换成字典格式
40                 dict_add = eval(info_add)  # 字符串转换成字典
41                 if dict_add["backend"] not in dict_file.keys():  # 判断新增的信息没有存在于文件中
42                     dict_add_record = dict_add["record"]  # 把要添加的信息定义到变量file_add 中
43                     file_add = "backend %s\n\t\tserver %s weight %s maxconn %s\n" % (dict_add["backend"],
44                                                                                      dict_add_record["server"],
45                                                                                      dict_add_record["weight"],
46                                                                                      dict_add_record["maxconn"],)
47                     file_haproxy.write(file_add)  # 把新增的信息写到文件中
48                     info_message("Add")  # 打印增加成功
49                     print("\33[32;1mSuccessfully adding information backend %s to a file\33[0m" % (dict_add["backend"]))
50                 else:  # 如果已经存在 打印信息已经存在
51                     print("\33[31;1mError:Add the information already exists!\33[0m")
52                 if_continue()
53             except Exception:  # 如果输入的字符不能转换为字典格式 提示错误
54                 print("\33[31;1mError:Please enter the dict format!\33[0m")
55                 if_continue()
56 
57         elif user_choice == "3":
58             info_del = input("\33[34;1mInput information to del:\33[0m")
59             try:  # 判断输入的类型是否可以转换成字典格式
60                 dict_del = eval(info_del)  # 字符串转换成字典
61                 if dict_del["backend"] in dict_file.keys():  # 判断要删除的信息有没有存在于文件中
62                     file_haproxy.seek(0)
63                     list_del = file_haproxy.readlines()  # 把文件信息写入列表list_del
64                     index = list_del.index("backend %s\n" % (dict_del["backend"]))  # 获取要删除信息的下标
65                     del list_del[index]  # 在列表中删除输入信息
66                     del list_del[index]
67                     file_haproxy.seek(0)
68                     file_haproxy.truncate(0)  # 文件清空
69                     for line in list_del:  # 把list_del内容写入到文件中
70                         file_haproxy.write(line)
71                     info_message("Del")  # 提示删除成功
72                     print("\33[32;1mSuccessfully delect information backend %s to a file\33[0m" % (dict_del["backend"]))
73 
74                 else:  # 如果要删除的信息不再文件中,打印信息不存在
75                     print("\33[31;1mError:Delect the information is not exists!\33[0m")
76                 if_continue()
77             except Exception:  # 如果输入的字符不能转换为字典格式 提示错误
78                 print("\33[31;1mError:Please enter the dict format!\33[0m")
79                 if_continue()
80 
81         elif user_choice == "q":
82             print("\33[31;1mExit\33[0m".center(30, "-"))
83             exit()
84 
85         else:
86             print("\33[31;1mError:Select the ID does not exist!\33[0m")

  执行效果:

 1 E:\Python\PythonExercising\haproxy\venv\Scripts\python.exe E:/Python/PythonExercising/haproxy/haproxy1.py
 2 *************File_Operations_Backend************** 
 3 1    Query
 4 2    Add
 5 3    Del
 6 q    Quit
 7 Select the ID to operate:1
 8 Input information to query:www.oldboy.org
 9 --------------Info of Query---------------
10 server 100.1.7.5 100.1.7.5 weight 20 maxconn 3000
11 
12 Do you want to continue to operate on files【y】/【n】:
13 y
14 *************File_Operations_Backend************** 
15 1    Query
16 2    Add
17 3    Del
18 q    Quit
19 Select the ID to operate:2
20 Input information to add:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}}
21 --------------Info of Add---------------
22 Successfully adding information backend www.baidu.com to a file
23 
24 Do you want to continue to operate on files【y】/【n】:
25 y
26 *************File_Operations_Backend************** 
27 1    Query
28 2    Add
29 3    Del
30 q    Quit
31 Select the ID to operate:3
32 Input information to del:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}}
33 --------------Info of Del---------------
34 Successfully delect information backend www.baidu.com to a file
35 
36 Do you want to continue to operate on files【y】/【n】:
37 y
38 *************File_Operations_Backend************** 
39 1    Query
40 2    Add
41 3    Del
42 q    Quit
43 Select the ID to operate:q
44 -------Exit--------
45 
46 Process finished with exit code 0

  总结提高:

  1、不断优化程序结果,更符合用户习惯;

   2、可以先将原文件遍历,将有关信息存储为字典,方便后续条件的判断;

   3、r+模式下,如果在.write()进行写入内容前,没有print()输出,则要写的内容会从文件头部开始写入;

          如果在.write()进行写入内容前,有print()输出,则要写的内容会从文件尾部开始写入。

   4、eval函数的用法:

    官方解释为:将字符串str当成有效的表达式来求值并返回计算结果。

    1、计算字符串中有效的表达式,并返回结果。

1 >>> eval('pow(2,2)')
2 4
3 >>> eval('2 + 2')
4 4

    2、将字符串转成相应的对象(如list、tuple、dict和string之间的转换)。

 1 >>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
 2 >>> b = eval(a)
 3 >>> b
 4 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
 5 >>> a = "{1:'xx',2:'yy'}"
 6 >>> c = eval(a)
 7 >>> c
 8 {1: 'xx', 2: 'yy'}
 9 >>> a = "(1,2,3,4)"
10 >>> d = eval(a)
11 >>> d
12 (1, 2, 3, 4)

      

转载于:https://www.cnblogs.com/visonwong/p/9012009.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值