首页 > 代码库 > 增删改查

增删改查

#查看
def look():
    while 1 :
        x = input("查看:如www.oldboy.org\n")#输入
        i = []#定义空列表
        flag = False#标识位
        with open("haproxy.conf") as x_read:#打开文件
            for line in x_read:
                if line.startswith("backend") and x in line:#判断输入是否在文件中并且是backend开头
                    flag = True
                    continue
                if line.startswith("backend") and flag:#判断标识位是否为真
                    break
                if flag:
                    i.append(line.strip())#把查看的内容写到列表中
            for s in i:
                print(s)

#删除
def delete():
    x = input("删除:如www.oldboy.org\n")#删除
    with open("haproxy.conf") as x_read,open("text",mode="w") as x_1:
        for line in x_read:
            x_1.write(line)
            if line.startswith("backend") and x in line:
                break



#修改
def amend():
    x = input("修改:如www.oldboy.org\n")
    d = "        server 1111 weight 2222 maxconn 3333"
    with open("haproxy.conf") as x_read,open("text",mode="w") as x_1:
        for line in x_read:
            x_1.write(line)
            if line.startswith("backend") and x in line:
                x_1.write(d)
                break



#增加
def add():
    x = input("增加:如www.oldboy.org\n")
    d = "        server 1111 weight 2222 maxconn 3333\n"
    with open("haproxy.conf") as x_read, open("text", mode="w") as x_1:
        for line in x_read:
            x_1.write(line)
            if line.startswith("backend") and x in line:
                x_1.write(d)

#列表
def  list():
    jyx = ‘‘‘
        1.look 查看:
        2.delete 删除:
        3.amend 修改:
        4.add 增加:
        0.exit 退出:
        ‘‘‘
    print(jyx)

def mains():
    list()
    a = input("你想干什么:")
    if a==1:
        look()
    if a==2:
        delete()
    if a==3:
        amend()
    if a==4:
        add()
    if a==0:
        exit()
mains()

 

增删改查