需求:在一个配置文件中找到匹配的内容,并进行insert和update的操作
python version:3.5
os:Windows 7
说明:这里只是当作案例库来使用,作为初学者,虚心接受大神们的各种蹂躏和摧残,有可以优化和不妥的地方请不吝吐槽一下~~
-----------------------------------------------------------------ha.conf----------------------------------------------------------------------------------
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.meta.org use_backend www.mate.org if www backend www.meta.org server 10.123.37.96 10.1.7.253 weight 10 maxconn 300 server 10.143.7.216 10.1.7.128 weight 20 maxconn 2000 backend buy.Meta.org server 10.127.57.18 10.1.27.198 weight 50 maxconn 1500----------------------------------------------------------------new.conf---------------------------------------------------------------------------------
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.meta.org use_backend www.mate.org if www backend www.meta.org server 10.123.37.96 10.1.7.253 weight 10 maxconn 300 server 10.143.7.216 10.1.7.128 weight 20 maxconn 2000 backend buy.Meta.org server 10.127.57.18 10.1.27.198 weight 50 maxconn 1500 backendwww.allen.org server 10.12.27.49 10.10.7.19 weight 30 maxconn 1000
-----------------------------------------------------------------code--------------------------------------------------------------------------------------
#!/usr/bin/env python
# -*- coding = utf-8 -*-
import shutil
#backend=www.allen.org
def fetch(backend):
#标签初始化
flag = False
result = []
with open('ha.conf','r') as f:
for line in f:
line_list=line.strip()
#找到目标开始行
if line.startswith('backend') and line_list == 'backend'+backend:
#修改标签
flag = True
continue
#找到结束行
if flag and line.startswith('backend'):
flag = False
break
#将中间行写入列表
if flag and line_list:
result.append(line_list)
return(result)
def update(backend,record):
record_list=fetch(backend)
#全部不存在
if not record_list:
with open("ha.conf", "r")as old, open('new.conf', 'a')as new:
for line in old:
new.write(line)
new.write( '\nbackend' + backend + '\n' )
new.write( " " * 8 + record + '\n' )
else:
#如果backend存在,record存在
if record in record_list:
shutil.copy('ha.conf', 'new.conf')
#如果backend存,record不存在
else:
record_list.append(record)
with open("ha.conf", "r")as old, open('new.conf', 'a')as new:
flag = False
for line in old:
#找到开始行
if line.startswith('backend') and line.strip() == 'backend' + backend:
flag = True
new.write(line)
#逐行写新增行
for new_list in record_list:
new.write( " "*8 + new_list + "\n" )
continue
#结束行
if flag and line.startswith('backend'+backend):
flag = False
new.write(line)
continue
if not flag and line.strip():
new.write(line)
bk= "www.allen.org"
rd = "server 10.12.27.49 10.10.7.19 weight 30 maxconn 1000"
update(bk,rd)
#遇到的问题,append是list的方法,对于文件,只能write,当时搞混了
#
#
# def main():
# a=input('choose what you want:1.fetch;2.update:')
# if a=='1':
# b=input('input which url you want to search(like:"www.oldboy.org"):')
# fetch('b')
# if a=='2':
# b = input('input which url you want to search(like:"www.oldboy.org"):')
# c = input('input the info(like:"server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000"):')
# update('b','c')
#
# if __name__ == '__main__':
# main()