d代码:

# __author__ = liukun
# coding:utf-8
line = input("please input the config \n :")
#提示输入字符
#输入{"backend":"www.oldboy.org","record":{"server":"100.1.70.9","weight":20,"maxconm":3000}}
a_dict = eval(line)#转换为字典
dict_b = a_dict['record']
line_b = a_dict['backend']
file_Read = open("config")#打开配置文件
file_Write = open("config.bak", 'w')#打开临时文件
file_test = file_Read.readline()#按行读取配置文件
while ('' != file_test):
line = file_test.split()
if line.count('backend') == 1 and line.count(a_dict['backend']) == 1 : #匹配域名所在行
file_Write.write('\n' + file_test)
string = "server %s weight %s maxconn %s" %(dict_b['server'], dict_b['weight'],dict_b['maxconm'])
#写入值太长,先用变量格式化.
file_Write.write('\t'*2 + string + '\n')#添加 两个tab保持缩进对齐,末尾换行
file_test = file_Read.readline()
else:
file_Write.write(file_test)
file_test = file_Read.readline()
file_Read.close()
file_Write.close()#遍历完毕,关闭文件
file_Read_2 = open("config.bak") #将 bak 文件内容回写源文件 config
file_Write_2 = open("config", 'w')
file_test_2 = file_Read_2.readline()
while ('' != file_test_2):
file_Write_2.write(file_test_2)
file_test_2 = file_Read_2.readline()
file_Read_2.close()
file_Write_2.close()

config:

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.oldboy.org
use_backend www.oldboy.org if www backend www.oldboy.org
server 100.1.70.9 weight 20 maxconn 31111
server 100.1.70.9 weight 20 maxconn 3000
server 100.1.70.9 weight 20 maxconn 3000
server 100.1.70.9 weight 20 maxconn 344444
server 100.1.70.9 weight 20 maxconn 3000
server 100.1.70.9 weight 20 maxconn 3000
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000

config 写入的更多相关文章

  1. python向config、ini文件读取写入

    config读取操作 cf = configparser.ConfigParser() # 实例化对象 cf.read(filename) # 读取文件 cf.sections() # 读取secti ...

  2. C# 程序修改config文件后,不重启程序刷新配置ConfigurationManager

    基本共识: ConfigurationManager 自带缓存,且不支持 写入. 如果 通过 文本写入方式 修改 配置文件,程序 无法刷新加载 最新配置. PS. Web.config 除外:Web. ...

  3. 配置Jenkins使用Gitlab的代码库进行构建

    1. 首先确认Jenkins上安装了Git plugin, 以及Subversion plugin Manage Jenkins -> Plugin Manager -> Availabl ...

  4. ZooKeeper的Znode剖析

    在ZooKeeper中,节点也称为znode.由于对于程序员来说,对zk的操作主要是对znode的操作,因此,有必要对znode进行深入的了解. ZooKeeper采用了类似文件系统的的数据模型,其节 ...

  5. ubuntu下ssh使用proxy:corkscrew

    1,安装corkscrew: sudo apt-get install corkscrew 2, 配置 vim ~/.ssh/config 写入如下: Host 10.1.*.* ProxyComma ...

  6. 『随笔』C# 程序 修改 ConfigurationManager 后,不重启 刷新配置

    基本共识: ConfigurationManager 自带缓存,且不支持 写入. 如果 通过 文本写入方式 修改 配置文件,程序 无法刷新加载 最新配置. PS. Web.config 除外:Web. ...

  7. node.js NPM 使用

    n=NPM是一个Node包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准.有了NPM,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包.npms安装: 下载npm源 ...

  8. 黑苹果引导工具 Clover 配置详解及Clover Configurator使用

    黑苹果引导工具 Clover 配置详解及Clover Configurator使用  2017-03-11 14:01:40 by SemiconductorKING 转自:@三个表哥   简介: 可 ...

  9. CentOS6.9下安装rabbitmq消息队列

    安装如下步骤: 首先安装erlang yum install erlang 安装rabbitmq rpm包 wget http://www.rabbitmq.com/releases/rabbitmq ...

随机推荐

  1. UVALive 5962 Strongly Connected Chemicals --最大独立集

    题意:给n个阳离子和m个阴离子,并给出相互的吸引关系,求一个最大的点集,使其中的每个阴阳离子相互吸引. 解法:枚举每条边,使该条边存在,然后建立反图,求一个最大匹配,此时的点数减去最大匹配与ans求一 ...

  2. 【hibernate】<第一节>hibernate简单入门

    所需工具: ide:eclipse or myeclipse jdk:1.7 jar包:hibernate-distribution-3.6.0.Final 和对应mysql的驱动类(对应jar包如图 ...

  3. PHP header函数的几大作用

    先看看官方文档的定义 (PHP 4, PHP 5, PHP 7) header - 发送原生 HTTP 头 void header ( string $string [, bool $replace ...

  4. GridControl控件绑定RepositoryItemImageComboBox 作为下拉框使用

    如果,时间长时了,已前做过的东西,都记不得了,所以记录一下. 废话不多说. 1.拖出gridview控件,然后将字段绑定上去 2.将要做下拉框的控件加入RepositoryItemImageCombo ...

  5. f2fs源码解析(五) node管理结构梳理

    node是f2fs重要的管理结构, 它非常重要! 系统挂载完毕后, 会有一个f2fs_nm_info结构的node管理器来管理node的分配. f2fs_nm_info中最让人疑惑的是几颗基数树: s ...

  6. WPF在XAML中Binding使用StringFormat属性

    1. 绑定Currency, 如果没有字符的话, =后面需要先加入{}. 不加的话会出问题. 1 <TextBlock Text="{Binding Amount, StringFor ...

  7. Linux 守护进程一

    守护进程是一个后台进程,它无需用户输入就能运行,经常是提供某种服务. LInux作为服务器,主要的进程也都是为系统或用户提供后台服务功能. 常见的守护进程有Web服务器.邮件服务器以及数据库服务器等等 ...

  8. C语言 二级指针内存模型①

    //二级指针第一种内存模型 #include<stdio.h> #include<stdlib.h> //说明:①:类似于int a[5]={0},数组名a是一维数组a中首元素 ...

  9. java中从1970-1-1到当前时间之间的毫秒数转换为oracle date

    java中System.currentTimeMillis()取到的是从1970-01-01 00:00:00.000到当前时间的毫秒数,一个long类型的值. 现在oracle数据库中某表中存取的是 ...

  10. PHP基础17:日期

    <?php //1.PHP Date()函数,PHP Date() 函数把时间戳格式化为更易读的日期和时间. // 获得简单的日期 echo "今天是".date(" ...