python--对配置文件进行搜索,增加新的内容

要求:

文件haproxy
1、查 输入:www.oldboy.org 获取当前backend下的所有记录
2、新建 输入: arg = {‘backend‘: ‘www.oldboy.org‘,‘record‘:{‘server‘: ‘100.1.7.9‘,‘weight‘: 20,‘maxconn‘: 30}}
实现:

1、文件open close readline write

2、字符串strip split

‘‘‘
1、查
    输入:www.oldboy.org
    获取当前backend下的所有记录

2、新建
    输入:
        arg = {‘backend‘: ‘www.oldboy.org‘,‘record‘:{‘server‘: ‘100.1.7.9‘,‘weight‘: 20,‘maxconn‘: 30}}
‘‘‘
def search(word,file=‘haproxy‘):
    fd=open(file,‘r‘,encoding=‘utf-8‘)
    data=fd.readline()
    while data:
        if word not in data or ‘backend‘ not in data:
            data=fd.readline()
        else:
            data=fd.readline()
            while data                     and ‘global‘ not in data                     and ‘defaults‘ not in data                     and ‘listen‘ not in data                     and ‘frontend‘ not in data:
                print(data)
                data=fd.readline()
    fd.close()
    return True

def addnew(word,file=‘haproxy‘):
    fd=open(file,‘a‘,encoding=‘utf-8‘)
    arg=eval(word)
    for key in arg:
        if key==‘global‘ or key==‘defaults‘ or key==‘listen‘ or key==‘frontend‘ or key==‘backend‘:
            fd.write(key+‘ ‘+arg[key]+‘\n‘)
        else:
            data= arg[key]
            wdata=‘‘
            for k in data:
                wdata=wdata+‘ ‘+k+‘ ‘+str(data[k])
            fd.write(‘\t‘+wdata+‘\n‘)
    fd.close()
    return

#脚本主程序
flag=True
while flag:
    choice=input("请输入您需要对haproxy文件进行的操作。\n1 查找 \n2 插入  \nq 退出\n")
    if choice==‘1‘:
        print("输入域名,可以找到backend下所有记录。")
        sword=input("请输入您要查找的域名,如:www.oldboy.org:")
        search(sword)
    elif choice==‘2‘:
        print("要插入的记录,写成字典格式。")
        sword=input("请输入:")
        addnew(sword)
    elif choice==‘q‘:
        flag=False
    else:
        print("我不知道您要干什么。")
时间: 2024-08-28 07:21:38

python--对配置文件进行搜索,增加新的内容的相关文章

python 之装饰器(用装饰器给现有函数增加新功能)

#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Mon Nov 14 01:01:29 2016 @author: toby """ #知识点:装饰器 ''' #一.小粒子: #要求1.假如产品经历要求在每个函数之上执行之前都添加一个验证的功能,当然这里只是模拟而已,别当真哈! #已写好的现有函数如下,有1000个函数 def func1():     print 'fun

Python中配置文件解析模块-ConfigParser

Python中有ConfigParser类,可以很方便的从配置文件中读取数据(如DB的配置,路径的配置).配置文件的格式是: []包含的叫section, section 下有option=value这样的键值该模块的常用方法 1.config=ConfigParser.ConfigParser() 创建ConfigParser实例 2.config.sections() 返回配置文件中节序列 3.config.options(section) 返回某个项目中的所有键的序列 4.config.g

SPRING IN ACTION 第4版笔记-第四章ASPECT-ORIENTED SPRING-010-Introduction为类增加新方法

一. 1.Introduction的作用是给类动态的增加方法 When Spring discovers a bean annotated with @Aspect , it will automatically create a proxy that delegates calls to either the proxied bean or to the introduction implementation, depending on whether the method called be

转载:Pixhawk源码笔记十一:增加新的MAVLink消息

转自:新浪长沙@WalkAnt 第十二部分 增加新的MAVLink消息 英文参考:http://dev.ardupilot.com/wiki/code-overview-adding-a-new-mavlink-message/ 本节源自:http://liung.github.io/blog/apm/2014-09-05-APM-增加新的MAVLink通讯协议消息.html MavLink协议:https://pixhawk.ethz.ch/mavlink/ 地面站之间的数据和指令通信都是通过

为Python添加默认模块搜索路径

为Python添加默认模块搜索路径 方法一:函数添加 1) import sys 2) 查看sys.path 3) 添加sys.path.append("c:\\") 方法二:修改环境变量 windows用户可以修改系统环境变量PYTHONPATH 方法三:增加.pth文件,推荐! 在site-packages添加一个路径文件,如mypkpath.pth,必须以.pth为后缀,写上你要加入的模块文件所在的目录名称就是了. 1) windows c:\python27\site-pack

python 读配置文件

python读conf配置文件--ConfigParser python读写配置文件还是比较方便得. 配置文件的格式是: []包含的叫section,    section 下有option=value这样的键值 配置文件   test.conf [section1] name = tank age = 28 [section2] ip = 192.168.1.1 port = 8080 1) 基本的读取配置文件     -read(filename) 直接读取ini文件内容     -sect

Hadoop概念学习系列之Hadoop集群动态增加新节点或删除已有某节点及复制策略导向

hadoop-2.6.0动态添加新节点 https://blog.csdn.net/baidu_25820069/article/details/52225216 Hadoop集群动态增加新节点 一.在新增节点配置运行环境 1.安装和其他节点相同的java环境,jdk版本要相同. 2.修改/etc/hosts配置文件,添加ip与hostname的对应关系并分发到集群各个节点. 3.关闭防火墙.相关软件工具的安装等. 4.配置ssh免密码登录,使新增节点和集群其他节点能实现免密码登录. 5.修改s

(笔记)Mysql命令grant on:增加新用户并控制其权限

grant on命令用于增加新用户并控制其权限. grant on命令格式:grant select on 数据库.* to 用户名@登录主机 identified by “密码”; 1) 增加一个用户test1,密码为abc,让他可以在任何主机上登录,并对所有数据库有查询.插入.修改.删除的权限.首先用root用户连入MYSQL,然后键入以下命令:    grant select,insert,update,delete on *.* to [[email protected]”%][emai

给zencart产品增加新字段

经常遇到一些产品具有很丰富的信息,可zencart后台添加产品的时候,就只有那么几个字段.例如产品model,产品库存等等. 想给某个产品定制一个像magento一样的短描述功能,或者想显示该产品在亚马逊上的链接. 这个方法就派上用场了. 我这里讲的就是如何给产品添加一个product_color字段. 1,先去phpmyadmin,找到你网站的数据库,然后找到products表,给该表添加一个product_color字段,不会用sql语句的可手动添加. 2,编辑文件admin/include