zoukankan      html  css  js  c++  java
  • day05.2-一个文件的增删改查实例

    一. 测试程序

     1 #INFO.txt源文件内容
     2 global
     3         log 127.0.1 local2
     4         daemon
     5         maxconn 256
     6         log 127.0.0.1 local2 info
     7 default
     8         log global
     9         mode http
    10         timeout connect 5000ms
    11         timeout client 50000ms
    12         timeout server 50000ms
    13         option dontlognull
    14 listen stats: 8888
    15         stats enable
    16         stats uri        /admin
    17         stats auth       admin:1234
    18 frontend oldboy.org
    19         bind 0.0.0.0:80
    20         option httplog
    21         option httpclose
    22         option forwardfor
    23         log global
    24         acl www hdr_reg(host) -i www.oldboy.org
    25         use_backend www.oldboy.org if www
    26 backend www.oldboy1.org
    27         server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
    28         server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
    29         server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
    30         server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
    31 backend www.oldboy2.org
    32         server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    33 backend www.oldboy20.org
    34         server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
      1 #bin.py
      2 
      3 """
      4 功能:查询文件中是否存在输入标签对应的信息,并打印查询结果
      5 输入:标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
      6 返回:True----文件中存在标签对应的信息       False----文件中不存在标签对应的信息
      7 """
      8 def fetch(record):
      9     record = eval(record)
     10     tag1 = False
     11     tag2 = False
     12     sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
     13     with open("INFO.txt","r") as file:
     14         for item in file:
     15             if item.strip()=="backend "+record["backend"]:
     16                 tag1 = True
     17             if tag1 and item.strip()==sub_record:
     18                 tag2 = True
     19                 break
     20         file.close()
     21     if tag1 and tag2:
     22         return True
     23     else:
     24         return False
     25 
     26 """
     27 功能:添加标签信息,
     28       如果要添加信息的backend存在,则在backend对应的子标签末尾添加inform对应信息
     29       如果要添加信息的backend不存在,则在文件末尾添加backend以及inform对应信息      
     30 输入:要添加的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
     31 """
     32 def add(record):
     33     record = eval(record)
     34     sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
     35     tag1 = False
     36     tag2 = False
     37     with open("INFO.txt","r") as file,
     38         open("INFO_new.txt","w") as file_new:
     39         for item in file:
     40             if not tag1:
     41                 file_new.write(item)
     42             if item.strip()=="backend "+record["backend"]:
     43                 tag1 = True
     44                 continue
     45             if tag1 and item.startswith(" "):
     46                 file_new.write(item)
     47             if tag1 and not item.startswith(" "):
     48                 file_new.write(" "*8+sub_record+"
    ")
     49                 file_new.write(item)
     50                 tag2 = True
     51                 tag1 = False
     52         if not tag2:
     53             file_new.write("
    backend "+record["backend"])
     54             file_new.write("
    "+" "*8+sub_record+"
    ")
     55         file.close()
     56         file_new.close()
     57 
     58 """
     59 功能:删除标签信息,
     60       如果要删除的标签信息存在,则删除对于标签,原文件中该标签之后的信息逐行上移一行
     61       如果要删除的标签信息不存在,则显示提示信息      
     62 输入:要删除的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
     63 """
     64 def delete(record):
     65     res = fetch(record)
     66     if not res:
     67         print("要删除的标签信息不存在")
     68         return
     69     else:
     70         tag1 = False
     71         record = eval(record)
     72         sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
     73         with open("INFO.txt","r") as file,
     74             open("INFO_new.txt","w") as file_new:
     75             for item in file:
     76                 if not tag1:
     77                     file_new.write(item)
     78                 if item.strip() == "backend " + record["backend"]:
     79                     tag1 = True
     80                 if tag1 and item.strip()!=sub_record:
     81                     file_new.write(item)
     82                 if tag1 and item.strip()==sub_record:
     83                     tag1 = False
     84                     continue
     85             file.close()
     86             file_new.close()
     87 
     88 """
     89 功能:修改标签信息,
     90       如果要修改的标签信息存在,则将原文件中该标签信息修改
     91       如果要修改的标签信息不存在,则显示提示信息      
     92 输入:要修改的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
     93       修改后标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
     94 """
     95 def modify(recorded,record):
     96     res = fetch(recorded)
     97     if not res:
     98         print("要修改的标签信息不存在")
     99         return
    100     else:
    101         recorded = eval(recorded)
    102         sub_recorded = "server "+str(recorded["inform"]["server"])+" "+str(recorded["inform"]["server"])+" weight "+str(recorded["inform"]["weight"])+" maxconn "+str(recorded["inform"]["maxconn"])
    103         record = eval(record)
    104         sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
    105         if recorded["backend"]!=record["backend"]:
    106             print("输入有误")
    107             return
    108         else:
    109             tag1 = False
    110             tag2 = False
    111             with open("INFO.txt", "r") as file, 
    112                 open("INFO_new.txt", "w") as file_new:
    113                 for item in file:
    114                     if item.strip()=="backend "+recorded["backend"]:
    115                         tag1 = True
    116                     elif tag1 and item.strip()==sub_recorded:
    117                         tag2 = True
    118                     if tag2:
    119                         file_new.write(" "*8+sub_record+"
    ")
    120                         tag1 = False
    121                         tag2 = False
    122                         continue
    123                     elif not tag2:
    124                         file_new.write(item)
    125                 file.close()
    126                 file_new.close()
    127 
    128 Menu_msg = {"1":fetch,"2":add,"3":delete,"4":modify}           #功能函数字典
    129 menu_msg = ["1:  fetch","2:  add","3:  delete","4:  modify","5:  exit"]
    130 
    131 while(True):
    132     print("*"*20)
    133     for item in menu_msg:
    134         print("   " + item)
    135     print("*"*20)
    136     opration = input("请输入您的操作:").strip()
    137     if opration=="1":
    138         record = input("请输入查询信息").strip()
    139         res=Menu_msg[opration](record)
    140         print(res)
    141     elif opration=="2":
    142         record = input("请输入要添加的信息").strip()
    143         Menu_msg[opration](record)
    144     elif opration=="3":
    145         record = input("请输入要删除的信息").strip()
    146         Menu_msg[opration](record)
    147     elif opration=="4":
    148         recorded = input("请输入要修改的信息").strip()
    149         record = input("请输入修改后信息").strip()
    150         Menu_msg[opration](recorded,record)
    151     elif opration=="5":
    152         break
    153     elif opration not in Menu_msg.keys():
    154         continue

    二. 功能测试

    • 查询   
    #程序运行过程
    
    ********************
       1:  fetch
       2:  add
       3:  delete
       4:  modify
       5:  exit
    ********************
    请输入您的操作:>? 1
    请输入查询信息>? {"backend":"www.oldboy1.org","inform":{"server":"100.1000.7.9","weight":20,"maxconn":30}}
    True
    • 添加 

         1. 添加子标签

    #程序运行过程
    
    ********************
       1:  fetch
       2:  add
       3:  delete
       4:  modify
       5:  exit
    ********************
    请输入您的操作:>? 2
    请输入要添加的信息>? {"backend":"www.oldboy1.org","inform":{"server":"100.1000.7.9","weight":11,"maxconn":30}}
    #INFO_new.txt修改后文件内容
    
    global
            log 127.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    default
            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 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
            server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
            server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
            server 100.1000.7.9 100.1000.7.9 weight 11 maxconn 30
    backend www.oldboy2.org
            server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    backend www.oldboy20.org
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333

         2. 添加新标签

    #程序运行过程
    
    ********************
       1:  fetch
       2:  add
       3:  delete
       4:  modify
       5:  exit
    ********************
    请输入您的操作:>? 2
    请输入要添加的信息>? {"backend":"www.oldboy3.org","inform":{"server":"100.1000.7.9","weight":20,"maxconn":30}}
    #INFO_new.txt修改后文件内容
    global
            log 127.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    default
            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 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
            server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
            server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
    backend www.oldboy2.org
            server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    backend www.oldboy20.org
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
    backend www.oldboy3.org
            server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
    • 删除
    #程序运行过程
    
    ********************
       1:  fetch
       2:  add
       3:  delete
       4:  modify
       5:  exit
    ********************
    请输入您的操作:>? 3
    请输入要删除的信息>? {"backend":"www.oldboy1.org","inform":{"server":"10.10.0.10","weight":9999,"maxconn":33333333333}}
    #INFO_new修改后文件内容
    global
            log 127.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    default
            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
    backend www.oldboy1.org
            server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
            server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
            server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
    backend www.oldboy2.org
            server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    backend www.oldboy20.org
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
    • 修改
    #程序运行过程
    
    ********************
       1:  fetch
       2:  add
       3:  delete
       4:  modify
       5:  exit
    ********************
    请输入您的操作:>? 4
    请输入要修改的信息>? {"backend":"www.oldboy1.org","inform":{"server":"10.10.0.10","weight":9999,"maxconn":33333333333}}
    请输入修改后信息>? {"backend":"www.oldboy1.org","inform":{"server":"10.10.0.10","weight":9999,"maxconn":1111}}
    #INFO_new.txt文件修改后内容
    global
            log 127.0.1 local2
            daemon
            maxconn 256
            log 127.0.0.1 local2 info
    default
            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 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 1111
            server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
            server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
    backend www.oldboy2.org
            server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
    backend www.oldboy20.org
            server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
  • 相关阅读:
    20191324读书笔记10
    20191324读书笔记十一
    实验三:个人贡献
    20191324读书笔记12
    CBMVC For Titanium Alloy 发布!
    让 PowerDesigner 支持 SQLite!
    在类库中调用资源文件实现国际化!
    理解依赖注入及其好处!
    CBMVC Titanium Framework 介绍
    .Net插件框架的实现及分析(二)
  • 原文地址:https://www.cnblogs.com/zizaijiapu/p/10796831.html
Copyright © 2011-2022 走看看