文件内容的增删改查

时间:2022-10-20 22:08:42
文件内容的增删改查文件内容的增删改查
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.oldboy1.org
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
源文件

一:逐步进行操作: 

1.查询功能:

  当用户输入www.oldboy1.org用户信息后,打印对应的server信息:
  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 

while 1:
l=[]
flag=1
m=input("please input your choise:").strip()
with open("test",encoding="utf8") as f:
for i in f:
if i.startswith("backend") and m in i:
flag=0
continue
if i.startswith("backend") and flag==0:
break
if flag==0:
l.append(i)
for p in l:
print(p.strip())

2.增加功能:
  当用户输入{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}
  在www.oldboy20.org下增加一条server信息,即文件中:
  backend www.oldboy20.org
  server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
  server 11111 weight 2222 maxconn 3333

while 1:
a=input("please input your choise:".strip())
dic=eval(a)
b=dic["backend"]
c=dic["record"]
flag=1
l=[]
s="\t\tserver %s weight %s maxconn %s \n" %(c["server"],c["weight"], c["maxconn"])
with open("haproxy.conf", encoding="utf-8") as f, open("test", "w", encoding="utf-8") as w:
for line in f:
w.write(line)
if line.startswith("backend") and b in line:
flag=0
continue
if line.startswith("backend") and b not in line and flag==0:
flag=1
continue
if flag==0:
l.append(s)
for line1 in l:
w.write(line1)
import os
os.rename("haproxy.conf","conf_bak")
os.rename("test","haproxy.conf")
os.remove("conf_bak")

3.删除功能
  当用户输入{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}
  会将www.oldboy20.org下的server 11111 weight 2222 maxconn 3333删除,
  在文件中如下:
  backend www.oldboy20.org
  server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333

while 1:
a=input("please input your choise:".strip())
dic=eval(a)
b=dic["backend"]
c=dic["record"]
flag=1
s="\t\tserver %s weight %s maxconn %s \n" %(c["server"],c["weight"], c["maxconn"])
with open("haproxy.conf", encoding="utf-8") as f, open("test", "w", encoding="utf-8") as w:
for line in f:
if line.startswith("backend") and b in line:
flag=0
if line.strip()==s.strip() and flag==0:
flag=1
continue
w.write(line)
import os
os.rename("haproxy.conf","conf_bak")
os.rename("test","haproxy.conf")
os.remove("conf_bak")

4.修改功能:
  当用户输入{"backend":"www.oldboy2.org","record":{"server":0000,"weight":0000,"maxconn":0000}}
  在文件中如下:
  backend www.oldboy2.org 
  server 0000 weight 0000 maxconn 0000

while 1:
a=input("please input your choise:".strip())
dic=eval(a)
b=dic["backend"]
c=dic["record"]
flag=1
s="\t\tserver %s weight %s maxconn %s \n" %(c["server"],c["weight"], c["maxconn"])
with open("haproxy.conf", encoding="utf-8") as f, open("test", "w", encoding="utf-8") as w:
for line in f:
if line.startswith("backend") and b in line:
w.write(line)
flag=0
continue
if flag==0:
flag=1
line=s
w.write(line)
import os
os.rename("haproxy.conf","conf_bak")
os.rename("test","haproxy.conf")
os.remove("conf_bak")

二:合并成函数:

  虽然功能可以简单的实现,不过感觉很烂。。。

 

def search():
l=[]
flag=1
m=input("请输入您得查询内容:").strip()
with open("haproxy.conf",encoding="utf8") as f:
for i in f:
if i.startswith("backend") and m in i:
flag=0
continue
if i.startswith("backend") and flag==0:
break
if flag==0:
l.append(i)
for p in l:
print(p.strip())
def add():
a = input("请输入您要添加的内容:".strip())
dic = eval(a)
b = dic["backend"]
c = dic["record"]
flag = 1
l = []
s = "\n\t\tserver %s weight %s maxconn %s \n" % (c["server"], c["weight"], c["maxconn"])
with open("haproxy.conf", encoding="utf-8") as f, open("test", "w", encoding="utf-8") as w:
for line in f:
w.write(line)
if line.startswith("backend") and b in line:
flag = 0
continue
if line.startswith("backend") and b not in line and flag == 0:
flag = 1
continue
if flag == 0:
l.append(s)
for line1 in l:
w.write(line1)
os.rename("haproxy.conf","conf_bak")
os.rename("test","haproxy.conf")
os.remove("conf_bak")
print("添加成功!")
def delete():
a=input("请输入您要删除的内容:".strip())
dic=eval(a)
b=dic["backend"]
c=dic["record"]
flag=1
s="\t\tserver %s weight %s maxconn %s \n" %(c["server"],c["weight"], c["maxconn"])
with open("haproxy.conf", encoding="utf-8") as f, open("test", "w", encoding="utf-8") as w:
for line in f:
if line.startswith("backend") and b in line:
flag=0
if line.strip()==s.strip() and flag==0:
flag=1
continue
w.write(line)
import os
os.rename("haproxy.conf","conf_bak")
os.rename("test","haproxy.conf")
os.remove("conf_bak")
print("删除成功!")
def change():
a=input("请输入您的新内容:".strip())
dic=eval(a)
b=dic["backend"]
c=dic["record"]
flag=1
s="\t\tserver %s weight %s maxconn %s \n" %(c["server"],c["weight"], c["maxconn"])
with open("haproxy.conf", encoding="utf-8") as f, open("test", "w", encoding="utf-8") as w:
for line in f:
if line.startswith("backend") and b in line:
w.write(line)
flag=0
continue
if flag==0:
flag=1
line=s
w.write(line)
import os
os.rename("haproxy.conf","conf_bak")
os.rename("test","haproxy.conf")
os.remove("conf_bak")
print("修改成功!")
def tell_msg():
msg="""
1:查询
2:添加
3:删除
4:更改
"""
print(msg)
cmd_dic={
"查询":search,
"添加":add,
"删除":delete,
"更改":change
}
import os
while True:
tell_msg()
choice=input("请输入您得选择:".strip())
cmd_dic[choice]()