第二周作业__增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第二周作业__增删改查相关的知识,希望对你有一定的参考价值。
增删改查
def add ():
d = query()
add = input("please input proxy your add:")
count = 0
f_first.seek(0)
for line in f_first:
if count == d[max(d)] :
f_second.write("\t\t"+add+"\n")
f_second.write(line)
count +=1
def change ():
d = query()
old = input("please input proxy your choice:")
new = input("please input new proxy;")
count = 0
f_first.seek(0)
for line in f_first:
count +=1
if count == d[old] :
f_second.write("\t\t" + new + "\n")
continue
f_second.write(line)
def delete():
d = query()
delete = input("please input proxy your want delete:")
count = 0
f_first.seek(0)
for line in f_first:
count += 1
if count == d[delete]:
continue
f_second.write(line)
def query ():
d = {}
flag = False
url = input("please input url:")
count = 0
for line in f_first:
count += 1
if line.startswith("backend") and url in line:
flag = True
continue
if line.startswith("backend") and flag:
flag = False
if flag:
d[line.strip()] = count
for i in d:
print(i)
return d
def main ():
menu = {1: "add", 2: "delete", 3: "change", 4: "query",5:"quit"}
for i in range(1,6):
print (i,menu[i])
action = int(input("choice:"))
if action ==5:quit()
list_url = []
with open("haproxy.conf", encoding="utf8") as f_first:
for line in f_first:
if line.startswith("backend"):
list_url.append(line.strip())
for i in list_url:
print (i.split(" ")[1])
return action
import os
with open("haproxy.conf", encoding="utf8") as f_first, open("haproxy1.conf", mode="w+",
encoding="utf8") as f_second:
choice = main ()
if choice == 1 : add()
if choice == 2 : delete()
if choice == 3 : change()
if choice == 4 : query ()
os.rename("haproxy.conf","haproxy_bak.conf")
os.rename("haproxy1.conf","haproxy.conf")
以上是关于第二周作业__增删改查的主要内容,如果未能解决你的问题,请参考以下文章