Day5.对文件的增删改查+三次登陆后锁定

作业1:对文件haproxy.conf进行增删改查:

haproxy.conf原文件内容如下:

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 1.1.1.1 1.1.1.1 weight 9999 maxconn 3333

作为小白还是有些许的成就感,但代码中还存在很多很多的bug,以后会逐渐完善,我的代码如下:

def delete():
    l=[]
    with open("haproxy.conf",encoding="utf8") as f_read:
        print('''请输入如下格式的内容:
{'backend':'www.oldboy20.org','record':{'server':'1.1.1.1 1.1.1.1','weight':9999,'maxconn':3333}}
             ''')
        arg=input("please input your url:").strip()
        arg=eval(arg)
        for line in f_read:
            if line.strip().startswith("server") and arg["record"]["server"] in line:
                l.append(line)
    with open("haproxy.conf", encoding="utf8") as f_read, open("test", mode="w", encoding="utf8") as f_write:
        for i in f_read:
            if i not in l:
                f_write.write(i)
    print("删除成功")
    # import os
    # os.rename("haproxy.conf", "haproxy.conf_bak")
    # os.rename("test","haproxy.conf")
def add():
    l=[]
    flag=False
    with open("haproxy.conf",encoding="utf8") as f_read, open("test", mode="w", encoding="utf8") as f_write:
        print('''请输入如下格式的内容:
{'backend':'www.oldboy20.org','record':{'server':'1.1.1.1 1.1.1.1','weight':9999,'maxconn':3333}}
                     ''')
        arg=input("please input your url:").strip()
        arg=eval(arg)
        for line in f_read:
            if line.startswith("backend") and arg["backend"] in line:
                l.append(arg["record"])
                line="".join([line.strip()+"\n"+"        server %s weight %s maxconn %s"\
                     %(arg["record"]["server"],arg["record"]["weight"],arg["record"]["maxconn"])+"\n"])
            f_write.write(line)
    print("增加成功")
def modify():
    l=[]
    flag=False
    with open("haproxy.conf",encoding="utf8") as f_read, open("test", mode="w", encoding="utf8") as f_write:
        print('''请输入如下格式的内容:
{'backend':'www.oldboy20.org','record':{'server':'1.1.1.1 1.1.1.1','weight':9999,'maxconn':3333}}
该脚本暂不能修改server的值
                     ''')
        arg=input("please input your url:").strip()
        arg=eval(arg)
        for line in f_read:
            if line.strip().startswith("server") and arg["record"]["server"] in line:
                l.append(arg["record"])
                flag=True
            if flag:
                line="        server %s weight %s maxconn %s"\
                     %(arg["record"]["server"],arg["record"]["weight"],arg["record"]["maxconn"])
            f_write.write(line)
    print("修改成功")
def see():
    print('''请输入如下格式的内容:
www.oldboy2.org
        ''')
    arg=input('please input your url:').strip()
    l=[]
    flag=False
    with open("haproxy.conf",encoding="utf8") as f_read:
        for line in f_read:
            if line.startswith("backend") and arg in line: 
                flag=True  
                continue
            if line.startswith("backend") and flag:  
                break
            if flag:     
                l.append(line.strip())
        for i in l:
            print(i)
    print("查看成功")
while True:
    print('''
-------------------
    删除--->删
    增加--->增
    修改--->改
    查看--->查
-------------------
        ''')
    need=input("your need>>>:").strip()
    if need=="":
        delete()
        continue
    if need=="":
        add()
        continue
    if need=="":
        modify()
        continue
    if need=="":
        see()
    else:
        print("请输入正确的操作指令!!!")

作业2:输入用户名密码,认证成功后显示欢迎信息,输错三次后锁定:

用户信息表如下:

aaa---123
bbb---456
ccc---789
ddd---666

代码如下(憋了一上午,让我深深的明白了缩进和语句位置的重要性!!!):

def lock(username):
    with open("heimingdan", "a") as f:
        f.write(username)
        f.write('\n')
# f_read=open("name+passwd",encoding="utf8")
# f_write=open("heimingdan", encoding="utf8", mode="r+")
# f_write.write("aaa\n")
with open("name+passwd",encoding="utf8") as f_read,open("heimingdan", encoding="utf8") as f_write:
    flag=True
    l=[]
    while flag :
        user_name=input("请输入您的用户名:")
        f_write.seek(0)
        for i in f_write:
            if i.strip() == user_name:
                print("此用户已被锁定")
                exit()
        user_pwd=input("请输入您的密码:")
        f_read.seek(0)
        for line in f_read:
            name,passwd=line.strip().split("---")
            # print(name,passwd)
            if user_name == name :
                l.append(user_name)
                if user_pwd == passwd:
                    print("您已成功登录,欢迎光临!")
                    flag=False
                    break
        if l.count(user_name)==3 and flag!=False:
            lock(user_name)

 

posted @ 2017-04-08 13:16  这个新手不太冷°  阅读(230)  评论(2编辑  收藏  举报