zoukankan      html  css  js  c++  java
  • python练习题_04

    import os
    
    def fetch(data):
        # print('33[1;43m这是查询功能33[0m')
        # print('33[1;43m用户数据是33[0m',data)
        backend_data='backend %s'%data
        with open('website.conf','r',encoding='utf-8') as read_f:
            tag = False
            ret = []
            for read_line in read_f:
                if  read_line.strip()==backend_data:
                    tag=True
                    continue
                if tag and read_line.startswith('backend'):
                    break
                if tag:
                    print(read_line,end='')
                    ret.append(read_line)
            return ret
    
    def add():
        pass
    
    def change(data):
        print('这是修改功能')
        backend= data[0]['backend']
        backend_data='backend %s'%backend
        old_sever_record='%s sever %s weight %s
    '%(' '*8,data[0]['record']['sever'],
                                                     data[0]['record']['weight'])
        new_sever_record='%s sever %s weight %s
    '%(' '*8,data[1]['record']['sever'],
                                                   data[1]['record']['weight'])
    
        res=fetch(backend)
        print('change函数',res)
    
        # if not res or old_sever_record not in res:
        #     return '你要修改的记录不存在'
        # else:
        index=res.index(old_sever_record)
        res[index]=new_sever_record
        res.insert(0,'%s
    '%backend_data)
        with open('website.conf','r') as read_f,
            open('website_new.conf','w') as write_f:
            tag=False
            has_wirte=False
            for read_line in read_f:
                if  read_line.strip()==backend_data:
                    tag=True
                    continue
                if tag and read_line.startswith('backend'):
                    tag=False
                if not tag:
                    write_f.write(read_line)
                else:
                    if  not has_wirte:
                        for record in res:
                            write_f.write(record)
    
    
    def delete():
        pass
    
    if __name__=='__main__':
        msg='''
        1.查询
        2.添加
        3.修改
        4.删除
        5.退出
        '''
        msg_dic={
            '1':fetch,
            '2':add,
            '3':change,
            '4':delete
        }
    
    
        while True:
            print(msg)
            choice=input('请输入你的选项:').strip()
            if not choice:continue
            if choice==5:break
            data=input('请输入你的数据:'.strip())
            if choice!='1':
                data=eval(data)
            res=msg_dic[choice](data)
            print(res)
    
    
    # [{'backend':'www.newboy2.org','record':{'sever':'1.1.1.8','weight':80}},{'backend':'www.newboy2.org','record':{'sever':'1.0.0.8','weight':100}}]
  • 相关阅读:
    《C# to IL》第一章 IL入门
    multiple users to one ec2 instance setup
    Route53 health check与 Cloudwatch alarm 没法绑定
    rsync aws ec2 pem
    通过jvm 查看死锁
    wait, notify 使用清晰讲解
    for aws associate exam
    docker 容器不能联网
    本地运行aws lambda credential 配置 (missing credential config error)
    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
  • 原文地址:https://www.cnblogs.com/Manuel/p/10655833.html
Copyright © 2011-2022 走看看