实现数据的增删改查

一,程序要求

    对后台的数据(以一个文件为例)实现简单的增删改查功能,语句的语法为提示信息的示例

二,代码部分

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Jiang Feng"
# Date: 2018/12/28
import os
def file_processing(backend_data, type, res = None):
    if type == 'grep':
        with open('aaa', 'r') as read_f:
            results = []
            tag = False
            for read_line in read_f:
                if read_line.strip() == backend_data:
                    tag = True
                    continue
                if tag and read_line.strip().startswith('backend'):
                    break
                if tag:
                    results.append(read_line.strip())
        return results

    elif type == 'change':
        with open('aaa','r') as read_f ,open('aaa_new','w') as write_f:
            tag = False
            for read_line in read_f:
                if read_line.strip() == backend_data:
                    write_f.write(read_line)
                    tag = True
                    for server in res:
                        server = ' ' * 8 + server
                        server = '%s\n' % server
                        write_f.write(server)
                    continue
                if tag and read_line.startswith('backend'):
                    tag = False
                if not tag:
                    write_f.write(read_line)


        os.rename('aaa', 'aaa.bak')
        os.rename('aaa_new', 'aaa')
        os.remove('aaa.bak')
        return '数据已经修改完成'
def add(data):
    backend = data[0]['backend']
    backend_data = 'backend %s' % backend
    adddata = data[0]['server']
    add_list = grep(backend)
    if adddata in add_list:
        print('你想要添加的数据已经存在')
    else:
        add_list.append(adddata)
        return file_processing(backend_data, type='change', res=add_list)
def remove(data):
    backend = data[0]['backend']
    backend_data = 'backend %s'%backend
    rmdata = data[0]['server']
    remove_list = grep(backend)
    if rmdata in remove_list:
        remove_list.remove(rmdata)
        return file_processing(backend_data, type='change', res=remove_list)
    else:
        print('你想要修改的数据不存在')
def change(data):
    backend = data[0]['backend']
    backend_data = 'backend %s'%backend
    old_data = data[0]['server']
    new_data = data[1]['server']
    grep_list = grep(backend)
    if not new_data or old_data not in grep_list:
        print('你想要修改的数据不存在')
    else:
        index = grep_list.index(old_data)
        grep_list[index] = new_data
        return file_processing(backend_data, type='change', res=grep_list)
def grep(data):
    backend_data = 'backend %s' %data
    return file_processing(backend_data, type='grep')
if __name__ == '__main__':
    tip = '''
        欢迎使用:请根据提示输入数字
        1:增(例:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}])     
        2:删(例:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}])
        3:改[{'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000'},\
        {'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 22222'}]
        4:查(例:www.baidu1.com)
        5:退出
    '''
    msg = {
        '1': add,
        '2': remove,
        '3': change,
        '4': grep,
    }
    while True:
        print(tip)
        choice = input('请输入你的选项:')
        if not choice: continue
        if choice == '5': break
        data = input('请输入代码:')
        if choice != '4':
            data = eval(data)
        res = msg[choice](data)
        print(res)

三,文件中的变量

backend:为了使用查询,获得查询功能返回的列表

backend_data:为了与后台数据进行判断

file_processing:文件处理函数,为了实现程序的解耦性四,附上仿制的后台数据

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 www.baidu.com
        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.baidu.com
backend www.baidu1.com
        server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30
        server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
        server 2.2.2.5 2.2.2.5 weight 20 maxcoon 3333
        server 2.2.2.7 2.2.2.7 weight 20 maxcoon 3333
backend www.baidu2.com
        server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
        server 2.2.22.221 2.2.22.221 weight 20 maxconn 3333
backend www.baidu3.com
        server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
        server 2.2.2.111 2.2.2.111 weight 20 maxconn 3333

测试的示例代码:

增:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}]     
删例:[{'backend':'www.baidu2.com','server':'server 2.2.2.9 2.2.2.9 weight 20 maxconn 4000'}]
改:[{'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000'},{'backend':'www.baidu1.com','server':'server 10.10.10.1 10.10.10.1 weight 22 maxconn 22222'}]
查:www.baidu1.com
 
posted @ 2018-12-29 17:28  better_feng  阅读(560)  评论(0编辑  收藏  举报