day05.2-一个文件的增删改查实例

时间:2022-01-12 19:34:39

一. 测试程序

 #INFO.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
 #bin.py

 """
功能:查询文件中是否存在输入标签对应的信息,并打印查询结果
输入:标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
返回:True----文件中存在标签对应的信息 False----文件中不存在标签对应的信息
"""
def fetch(record):
record = eval(record)
tag1 = False
tag2 = False
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
with open("INFO.txt","r") as file:
for item in file:
if item.strip()=="backend "+record["backend"]:
tag1 = True
if tag1 and item.strip()==sub_record:
tag2 = True
break
file.close()
if tag1 and tag2:
return True
else:
return False """
功能:添加标签信息,
如果要添加信息的backend存在,则在backend对应的子标签末尾添加inform对应信息
如果要添加信息的backend不存在,则在文件末尾添加backend以及inform对应信息
输入:要添加的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
"""
def add(record):
record = eval(record)
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
tag1 = False
tag2 = False
with open("INFO.txt","r") as file,\
open("INFO_new.txt","w") as file_new:
for item in file:
if not tag1:
file_new.write(item)
if item.strip()=="backend "+record["backend"]:
tag1 = True
continue
if tag1 and item.startswith(" "):
file_new.write(item)
if tag1 and not item.startswith(" "):
file_new.write(" "*8+sub_record+"\n")
file_new.write(item)
tag2 = True
tag1 = False
if not tag2:
file_new.write("\nbackend "+record["backend"])
file_new.write("\n"+" "*8+sub_record+"\n")
file.close()
file_new.close() """
功能:删除标签信息,
如果要删除的标签信息存在,则删除对于标签,原文件中该标签之后的信息逐行上移一行
如果要删除的标签信息不存在,则显示提示信息
输入:要删除的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
"""
def delete(record):
res = fetch(record)
if not res:
print("要删除的标签信息不存在")
return
else:
tag1 = False
record = eval(record)
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
with open("INFO.txt","r") as file,\
open("INFO_new.txt","w") as file_new:
for item in file:
if not tag1:
file_new.write(item)
if item.strip() == "backend " + record["backend"]:
tag1 = True
if tag1 and item.strip()!=sub_record:
file_new.write(item)
if tag1 and item.strip()==sub_record:
tag1 = False
continue
file.close()
file_new.close() """
功能:修改标签信息,
如果要修改的标签信息存在,则将原文件中该标签信息修改
如果要修改的标签信息不存在,则显示提示信息
输入:要修改的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
修改后标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
"""
def modify(recorded,record):
res = fetch(recorded)
if not res:
print("要修改的标签信息不存在")
return
else:
recorded = eval(recorded)
sub_recorded = "server "+str(recorded["inform"]["server"])+" "+str(recorded["inform"]["server"])+" weight "+str(recorded["inform"]["weight"])+" maxconn "+str(recorded["inform"]["maxconn"])
record = eval(record)
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
if recorded["backend"]!=record["backend"]:
print("输入有误")
return
else:
tag1 = False
tag2 = False
with open("INFO.txt", "r") as file, \
open("INFO_new.txt", "w") as file_new:
for item in file:
if item.strip()=="backend "+recorded["backend"]:
tag1 = True
elif tag1 and item.strip()==sub_recorded:
tag2 = True
if tag2:
file_new.write(" "*8+sub_record+"\n")
tag1 = False
tag2 = False
continue
elif not tag2:
file_new.write(item)
file.close()
file_new.close() Menu_msg = {"":fetch,"":add,"":delete,"":modify} #功能函数字典
menu_msg = ["1: fetch","2: add","3: delete","4: modify","5: exit"] while(True):
print("*"*20)
for item in menu_msg:
print(" " + item)
print("*"*20)
opration = input("请输入您的操作:").strip()
if opration=="":
record = input("请输入查询信息").strip()
res=Menu_msg[opration](record)
print(res)
elif opration=="":
record = input("请输入要添加的信息").strip()
Menu_msg[opration](record)
elif opration=="":
record = input("请输入要删除的信息").strip()
Menu_msg[opration](record)
elif opration=="":
recorded = input("请输入要修改的信息").strip()
record = input("请输入修改后信息").strip()
Menu_msg[opration](recorded,record)
elif opration=="":
break
elif opration not in Menu_msg.keys():
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