Python 연습 - 온라인으로 구성 파일 수정

4719 단어 python 연습
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# author:stealth
import json,os

def fetch(backend):
    backend_title = 'backend %s' %backend
    record_list = []
    with open('ha.conf','r',encoding='utf-8') as obj:
        flag = False
        for line in obj:
            line = line.strip()
            if line == backend_title:
                flag = True
                continue
            if flag and line.startswith('backend'):
                flag = False
                break
            if flag and line:
                record_list.append(line)

    return record_list

def add(dict_info):
    backend = dict_info.get('backend')
    record_list = fetch(backend)
    backend_title = 'backend %s' %backend
    current_record = 'server %s %s weight %s maxconn %s' %(dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
    if not record_list:
        record_list.append(backend_title)
        record_list.append(current_record)
        with open('ha.conf','r') as read_f,open('ha.conf.new','w') as write_f:
            for line in read_f:
                write_f.write(line)
            for i in record_list:
                if i.startswith('backend'):
                    write_f.write(i+'
') else: write_f.write('%s%s
' %(8*' ',i)) else: record_list.insert(0,backend_title) if current_record not in record_list: record_list.append(current_record) with open('ha.conf', 'r') as read_f, open('ha.conf.new', 'w') as write_f: flag = False has_write = False for line in read_f: line_strip = line.strip() if line_strip == backend_title: flag = True continue if flag and line_strip.startswith('backend'): flag = False if not flag: write_f.write(line) else: if not has_write: for i in record_list: if i.startswith('backend'): write_f.write(i+'
') else: write_f.write('%s%s
' %(8*' ',i)) has_write = True def remove(dict_info): backend = dict_info.get('backend') record_list = fetch(backend) backend_title = 'backend %s' %backend current_record = 'server %s %s weight %s maxconn %s' % (dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn']) if not record_list: return else: if current_record not in record_list: return else: del record_list[record_list.index(current_record)] if len(record_list) > 0: record_list.insert(0,backend_title) with open('ha.conf') as read_f,open('ha.conf.new','w') as write_f: flag = False has_write = False for line in read_f: line_strip = line.strip() if line_strip == backend_title: flag = True continue if flag and line_strip.startswith('backend'): flag = False if not flag: write_f.write(line) else: if not has_write: for i in record_list: if i.startswith('backend'): write_f.write(i+'
') else: write_f.write('%s%s
' %(8*' ',i)) has_write = True if __name__ == '__main__': print('1、 ;2、 ;3、 ') num = input(' :') if num == '1': data = input(' :') print(fetch(data)) elif num == '2': data = input(' :') dict_data = json.loads(data) add(dict_data) elif num == '3': data = input(' :') dict_data = json.loads(data) remove(dict_data) else: print(' !')

좋은 웹페이지 즐겨찾기