python在线编辑文件_python--如何在线上环境优雅的修改配置文件?

1、如何在线上环境优雅的修改配置文件?

原配置文件

#原配置文件

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.cnblogs.com/chenlin163/

use_backend www.cnblogs.com/chenlin163/ if www

backend www.cnblogs.com/chenlin163/

server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000

原配置文件

1、查

输入:www.cnblogs.com/chenlin163/

获取当前backend下的所有记录

2、新建

输入:

arg = {

'bakend': 'www.cnblogs.com/chenlin163/',

'record':{

'server': '100.1.7.9',

'weight': 20,

'maxconn': 30

}

}

3、删除

输入:

arg = {

'bakend': 'www.cnblogs.com/chenlin163/',

'record':{

'server': '100.1.7.9',

'weight': 20,

'maxconn': 30

}

}

需求

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import json

import os

def fetch(backend):

backend_title = 'backend %s' % backend

record_list = []

with open('ha') 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 %d maxconn %d" % (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') as read_file, open('ha.new', 'w') as write_file:

flag = False

for line in read_file:

write_file.write(line)

for i in record_list:

if i.startswith('backend'):

write_file.write(i+'\n')

else:

write_file.write("%s%s\n" % (8*" ", i))

else:

record_list.insert(0, backend_title)

if current_record not in record_list:

record_list.append(current_record)

with open('ha') as read_file, open('ha.new', 'w') as write_file:

flag = False

has_write = False

for line in read_file:

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_file.write(line)

else:

if not has_write:

for i in record_list:

if i.startswith('backend'):

write_file.write(i+'\n')

else:

write_file.write("%s%s\n" % (8*" ", i))

has_write = True

os.rename('ha','ha.bak')

os.rename('ha.new','ha')

def remove(dict_info):

backend = dict_info.get('backend')

record_list = fetch(backend)

backend_title = "backend %s" % backend

current_record = "server %s %s weight %d maxconn %d" % (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') as read_file, open('ha.new', 'w') as write_file:

flag = False

has_write = False

for line in read_file:

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_file.write(line)

else:

if not has_write:

for i in record_list:

if i.startswith('backend'):

write_file.write(i+'\n')

else:

write_file.write("%s%s\n" % (8*" ", i))

has_write = True

os.rename('ha','ha.bak')

os.rename('ha.new','ha')

if __name__ == '__main__':

"""

print '1、获取;2、添加;3、删除'

num = raw_input('请输入序号:')

data = raw_input('请输入内容:')

if num == '1':

fetch(data)

else:

dict_data = json.loads(data)

if num == '2':

add(dict_data)

elif num == '3':

remove(dict_data)

else:

pass

"""

#data = "www.cnblogs.com/chenlin163/"

#fetch(data)

#data = '{"backend": "tettst.oldboy.org","record":{"server": "100.1.7.90","weight": 20,"maxconn": 30}}'

#dict_data = json.loads(data)

#add(dict_data)

#remove(dict_data)

demo

2、文件处理中xreadlines的内部是如何实现的呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值