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的内部是如何实现的呢?

python--如何在线上环境优雅的修改配置文件?的更多相关文章

  1. Python的在线编辑环境

    另外,再提供几个Python的在线编辑环境,可以直接写代码并且运行的环境. 在线Python实验室:http://www.pythoner.cn/labs/ 在线Python编辑器:http://ww ...

  2. BUG在线上环境中出现的原因总结

    1.线上环境数据的复杂度以及数据量是测试环境不能比拟的. 2.业务操作的不可控性,用户错误的使用习惯. 3.实际场景的复杂性. 上线之后,测试人员需要做好以下二件事:   第一,灰度测试 项目上线之后 ...

  3. Appium+python自动化15-在Mac上环境搭建

    前言 mac上搭建appium+python的环境还是有点复杂的,需要准备的软件 1.nodejs 2.npm 3.cnpm 4.appium 5.pip 6.Appium-Python-Client ...

  4. Appium+python自动化15-在Mac上环境搭建【转载】

    前言 mac上搭建appium+python的环境还是有点复杂的,需要准备的软件 1.nodejs 2.npm 3.cnpm 4.appium 5.pip 6.Appium-Python-Client ...

  5. python的线上环境配置

    1.安装python 2.7   http://www.cnblogs.com/strikebone/p/3970512.html 2.安装相关前置工具  pip, Django http://www ...

  6. Appium+python自动化16-appium1.6在mac上环境搭建启动ios模拟器上Safari浏览器

    前言 在mac上搭建appium踩了不少坑,先是版本低了,启动后无限重启模拟器.后来全部升级最新版本,就稳稳的了. 环境准备: 1.OS版本号10.12 2.xcode版本号8.3.2 3.appiu ...

  7. appium1.6在mac上环境搭建启动ios模拟器上Safari浏览器 转自:上海-悠悠

    前言 在mac上搭建appium踩了不少坑,先是版本低了,启动后无限重启模拟器.后来全部升级最新版本,就稳稳的了. 环境准备: 1.OS版本号10.12 2.xcode版本号8.3.2 3.appiu ...

  8. [Nginx笔记]关于线上环境CLOSE_WAIT和TIME_WAIT过高

    运维的同学和Team里面的一个同学分别遇到过Nginx在线上环境使用中会遇到TIME_WAIT过高或者CLOSE_WAIT过高的状态 先从原因分析一下为什么,问题就迎刃而解了. 首先是TIME_WAI ...

  9. python查询修改配置文件功能

    阅读目录 一.python查询功能代码 1.查询修改配置文件 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 i ...

随机推荐

  1. 与Cookie相比,Web Storage存在的优势

    与Cookie相比,Web Storage存在不少的优势,概括为以下几点:1. 存储空间更大:能提供5MB的存储空间(不同浏览器的提供的空间不同),Cookie仅4KB2. 存储内容不会发送到服务器: ...

  2. JAVA常用知识总结(二)

    JAVA中的参数传递总结先看两道笔试题: public class Test2 { public static void main (String [] args) { StringBuffer a ...

  3. AJPFX谈Java 性能优化之基本类型 vs 引用类型

    ★名词定义 先明确一下什么是“基本类型”,什么是“引用类型”. 简单地说,所谓基本类型就是 Java 语言中如下的8种内置类型: booleancharbyteshortintlongfloatdou ...

  4. Spring Boot学到的内容

    Hello World:了解程序入口(创建启动类) Web程序:写Controller类(@RestController),写Controller方法(@GetMapping),maven依赖spri ...

  5. 初识node,原理与浏览器何其相似

    话不多说,直接上图. 今日入手开始学习Nodejs,加油吧,小小前端的大V梦ヾ(◍°∇°◍)ノ゙

  6. jQuery Validate自定义各种验证方法(转)

    一.封装自定义验证方法-validate-methods.js /***************************************************************** j ...

  7. coursera网站中的VTT字幕的使用

    coursera网站中的VTT字幕的使用 1.https://www.coursera.org/learn/os-virtsecurity/lecture/xuWgP/1-3-cao-zuo-xi-t ...

  8. Java练习题02

    问题: 编程求一个整数数组的最大值.最小值.平均值和所有元素的和. 代码: public class Page99{     public static void main(String args[] ...

  9. 图解GitHub

    转自:http://marklodato.github.io/visual-git-guide/index-zh-cn.html 个人觉得这一篇比一些入门教程更值得看,图解很详细到位,很容易理解其工作 ...

  10. C#读取web.config配置文件内容

    1.对配置文件的访问. 方法一: string myConn =System.Configuration.ConfigurationManager.ConnectionStrings["sq ...