• 第三周作业


    HAproxy配置文件操作

      1 #!/usr/bin/env python
      2 
      3 # _*_ coding:utf-8 _*_
      4 import json,os
      5 
      6 def fetch(backend):
      7     flag = False
      8     fetch_list = []
      9     with open('b.txt') as obj:
     10         for line in obj:
     11             if line.strip() == "backend %s"%(backend):
     12                 flag = True
     13                 continue
     14             if line.strip().startswith("backend"):
     15                 flag = False
     16             if flag and line.strip():
     17                 fetch_list.append(line.strip())
     18     return fetch_list
     19 
     20 
     21 def add(dict_info):
     22 
     23     backend_title = dict_info.get("backend")
     24     context_title = "backend %s" % (backend_title)
     25     record_title = dict_info["backend"]
     26     context_record = "server %s %s weight %s maxconn %s" %(record_title["server"],record_title["server"],record_title["weight"],record_title["maxconn"])
     27     fetch_list = fetch(backend_title)
     28 
     29     if fetch_list:
     30         flag =False
     31         has_write = False
     32         with open('b.txt') as read_obj,open('b.new.txt','w')as write_obj:
     33             for line in read_obj:
     34                 if line.strip() == context_title:
     35                     flag = True
     36                     continue
     37                 if flag and line.startswith("backend"):
     38                     flag = False
     39                 if flag:
     40                     for new_line in fetch_list:
     41                         if not has_write:
     42                             temp = '%s%s'%(" "*8,new_line)
     43                             write_obj.write(temp)
     44                             has_write = True
     45                 else:
     46                     write_obj.write(line)
     47 
     48     else:
     49         with open('b.txt')as read_obj,open('b.new.txt','w')as write_obj:
     50             for line in read_obj:
     51                 write_obj.write(line)
     52             write_obj.write('
    '+context_title+'
    ')
     53             temp = " "*8+context_record+'
    '
     54             write_obj.write(temp)
     55 
     56     os.rename('b.txt','b.bak.txt')
     57     os.rename('b.new.txt','b.txt')
     58 
     59 
     60 def delete(dict_info):
     61     del_backend = dict_info["backend"]
     62     del_record = dict_info ["crecord"]
     63 
     64     context_title = "backend %s" %(del_backend)
     65     context_record = "server %s %s weight %s maxconn %s"%(del_record["server"],del_record["server"],del_record["weight"],del_record["maxconn"])
     66 
     67     fecth_list = fetch(del_backend)
     68 
     69     if not fecth_list:
     70         return
     71     else:
     72         if context_record not in fecth_list:
     73             print('mei you zhao dao')
     74             return
     75         else:
     76             fecth_list.remove(context_record)
     77 
     78         with open('b.txt','r')as read_obj,open('b.new.txt','w')as write_obj:
     79             flag = False
     80             has_write = False
     81             for line in read_obj:
     82                 if line.strip() == context_title:
     83                     write_obj.write(line)
     84                     flag = True
     85                     continue
     86                 if flag and line.startswith("backend"):
     87                     flag = False
     88                 if flag:
     89                     if not has_write:
     90                         print(fecth_list)
     91                         for new_line in fecth_list:
     92                             temp = '%s%s 
    '%(' '*8,new_line)
     93                             write_obj.write(temp)
     94                         has_write = True
     95                 else:
     96                     write_obj.write(line)
     97 
     98     os.rename('b.txt','b.bak.txt')
     99     os.rename('b.new.txt','b.txt')
    100 """
    101 s = '{"backend":"ttt.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":"3000"}}'
    102 data_info=json.loads(s)
    103 add(data_info)
    104 delete(data_info)
    105 """
    106 if __name__ == "__main__":
    107     print('1.获取记录 2.添加记录 3.删除记录')
    108     select_num = input('plz input backend:')
    109 
    110     if select_num == "1":
    111         backend = input('plz input backend:')
    112         fetch_list = fetch(backend)
    113         for i in fetch_list:
    114             print(i)
    115         else:
    116             print("shu ru cuo wu")
    117             data_str = input("plz input record>>>")
    118             data_dict = json.loads(data_str)
    119             if select_num == "2":
    120                 add(data_dict)
    121             elif select_num == "3":
    122                 delete(data_dict)
    123             else:
    124                 print("cuo wu")
    View Code
  • 相关阅读:
    Jmeter -准备篇
    SQL-DELETE
    SQL-UPDATE
    SQL- AND & OR & Order by & INSERT INTO
    SQL-WHERE
    SQL- select distinct
    SQL-select
    【图】max51开发板top页
    simple_spi_top
    UART.V
  • 原文地址:https://www.cnblogs.com/hanwei999/p/6058894.html
Copyright © 2020-2023  润新知