python3 haproxy配置文件操作练习

python3 haproxy配置文件操作练习

哈哈 老规矩 先来一个NLP第六条:咳咳!

六,重复旧的做法,只会得到旧的结果

  做法有不同,结果才会有不同。

  如果,你的事没有结果,改变你的做法。任何新的做法,都比旧的多一份成功的机会。

  想明天比昨天更好,必须用与昨天不同的做法。

  改变自己,别人才有可能改变。

  世界上本来便是每样事物都在不停地改变中,不肯改变的便面临淘汰或失败的威胁。

  因此只有不断地改变做法,才能保持与其他事物关系有理想状态。

  “做法”是规条,目的是取得价值,实现信念。维持最有效果地规条,就是灵活地不断修正做法,才能保证取得价值,实现信念。

  改变是所有进步的起点。


然后是今天的学习内容。。。也没干啥,最近有点飘还是沉不下心来,把以前的一个文件操作的作业给做了haproxy是啥,不知道。。。  然后还有个ATM机的作业,动不了手,得慢慢琢磨。草稿如下:

这是配置文件:

 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 backend www.oldboy.org
29         server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
30 
31 backend www.baidu.com
32         server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000
33 
34 backend www.4399.com
35         server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000
36 
37 backend www.7k7k.com
38         server 100.1.7.8 100.1.7.8 weight 29 maxconn 3080
View Code

这是草稿代码:

  1 #!usr/bin/env/ python
  2 # -*- coding:utf-8 -*-
  3 # Author: XiaoFeng
  4 
  5 
  6 def print_list():
  7     for index, i in enumerate(list1):
  8         print(index, i)
  9 
 10 
 11 def search():  #
 12     website = input("请输入您要查询的内容: (就输这个:www.oldboy.org)")
 13     find_str = "backend {}".format(website)    # format()  与{}配合使用
 14     with open("config_test", "r", encoding="utf-8") as f:
 15         for line in f:
 16             if find_str == line.strip():   # strip()用来去除左右两端的字符,空格换行啥的
 17                 line = next(f)
 18                 while line.strip().startswith("server"):  # startswith() 判断以啥开头
 19                     try:
 20                         print(line)
 21                         line = next(f)
 22                     except StopIteration:
 23                         break
 24                 break
 25         else:
 26             print("没得你要滴东西")
 27 
 28 
 29 def add():    # eval() 用来把字符转换成字典
 30     arg = eval(input("请输入您要写入的内容:"
 31                      "例如:{'backend': 'www.4399.com',"
 32                      "'record': {'server': '100.1.7.8',"
 33                                 "'weight': 20,"
 34                                 "'maxconn': 3000}}").strip())
 35     backend = "backend {}".format(arg["backend"])
 36     record = arg["record"]
 37     record_context = "server {0} {0} weight {1} maxconn {2}"
 38         .format(record["server"], record["weight"], record["maxconn"])
 39     add_flag = True
 40     with open("config_test", "r+", encoding="utf-8") as f:
 41         for line in f:
 42             if line.strip() == backend:
 43                 print("此网址已存在!")
 44                 add_flag = False
 45         if add_flag:   # 经过循环 文件指针已经在最下方了
 46             f.write("
{}".format(backend))
 47             f.write("
		{}".format(record_context))
 48 
 49 
 50 def update():
 51     arg = eval(input("请输入您要写入的内容:"
 52                      "例如:{'backend': 'www.7k7k.com',"
 53                      "'record': {'server': '100.1.7.8',"
 54                      "'weight': 20,"
 55                      "'maxconn': 3000}}").strip())
 56     backend = "backend {}".format(arg["backend"])
 57     record = arg["record"]
 58     record_context = "server {0} {0} weight {1} maxconn {2}" 
 59         .format(record["server"], record["weight"], record["maxconn"])
 60 
 61     update_flag = False
 62     reupdate_flag = False
 63     with open("config_test", "r", encoding="utf-8") as f, 
 64             open("config_test_bak", "w", encoding="utf-8") as f1:
 65         for line in f:
 66             if line.strip() == backend:
 67                 update_flag = True
 68                 continue
 69             if line.strip().startswith("backend") and backend != line.strip():
 70                 update_flag = False
 71             if not update_flag:
 72                 f1.write(line)
 73             if update_flag and not reupdate_flag:
 74                 f1.write(backend)
 75                 f1.write("
		{}".format(record_context))
 76                 reupdate_flag = True
 77     with open("config_test", "w", encoding="utf-8") as f, 
 78             open("config_test_bak", "r", encoding="utf-8") as f1:
 79         for line in f1:
 80             f.write(line)
 81 
 82 
 83 def delete():  #
 84     arg = input("请输入您要删除的内容:"
 85                      "例如:www.4399.com").strip()
 86     delet_flage = False
 87     with open("config_test", "r", encoding="utf-8") as f,
 88             open("config_test_bak", "w", encoding="utf-8") as f1:
 89         for line in f:
 90             if "backend {}".format(arg) == line.strip():
 91                 print(line)
 92                 delet_flage = True
 93                 continue
 94             if line.strip().startswith("backend") and "backend {}".format(arg) != line.strip():
 95                 delet_flage = False
 96             if not delet_flage:
 97                 f1.write(line)
 98     with open("config_test", "w", encoding="utf-8") as f, 
 99             open("config_test_bak", "r", encoding="utf-8") as f1:
100         for line in f1:
101             f.write(line)
102 
103 
104 def ifcontinue():
105     key = input("