文件操作练习:http://www.cnblogs.com/wupeiqi/articles/4950799.html
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.7.9 100.1.7.9 weight 20 maxconn 3000 backend buy.oldboy.org server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000 复制代码
要求:
输出: 1、获取ha记录 2、增加ha记录 3、删除ha记录 num = raw_input(‘请输入操作序号:‘) 如果用户输入的 1: read = raw_input(‘请输入backend:‘) 如输入:www.oldboy.org 讲配置文件 backend www.oldboy.org 节点下的所有记录获取到,并输入到终端 如果用户输入的 2: read = raw_input(‘请输入要新加的记录:‘) 如: {"backend": "test.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 30}} # 讲read字符串转换成 字典类型 read_dict = json.loads(read) backend_title = read_dict[‘backend‘] # 去配置文件中找到指定的节点: # backend test.oldboy.org # 如果已经存在, # 则在此节点下添加根据用输入构造出的记录,例如: server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 # 如果不存在, # 则添加backend节点和记录,例如: backend test.oldboy.org server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 # (可选)可以再对节点下记录进行判断是否已经存在 如果用户输入的 3: read = raw_input(‘请输入要删除的记录:‘) 如: {"backend": "test.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 30}} # 讲read字符串转换成 字典类型 read_dict = json.loads(read) backend_title = read_dict[‘backend‘] #去配置文件中找到指定的节点,并在删除指定记录,如: backend test.oldboy.org server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 # 删除掉 # (可选)如果backend下所有的记录都已经被删除,那么将当前 backend test.oldboy.org 也删除掉。
参考思路:
http://www.cnblogs.com/wupeiqi/articles/5539371.html
#一行一行读 f=open(xxx) for line in f: print(line) def fetch(backend): result=[] with open("ha.conf",‘r‘,encoding="utf-8") as f: flag = False for line in f: if line.strip().startwith("backend") and line.strip() == "backend "+backend flag = True continue if flag==True and line.strip().startwith("backend"): flag=False break if flag and line.strip(): result.append(line.strip()) return result def add(backend,record): #思路一: #先检查记录是否存在 record_list=fetch(backend) if not record_list: #backend不存在 with open(‘ha.conf‘,r) as old,open("new.conf",‘w‘) as new: for line in old: new.write(line) new.write("backend "+backend +"\n") new.write(" " * 8 + record + "\n") else: #backend存在 #如果记录存在,不用做任何操作,如果不存在,则将记录添加到backend下 if record in record_list: #记录存在 import shutil shutil.copy("ha.conf","new.conf") pass else: #backend存在,record不存在 record_list.append(record) with open(‘ha.conf‘,r) as old,open("new.conf",‘w‘) as new: flag = False for line in old: if line.strip().startwith("backend") and line.strip() == "backend "+backend: flag = True new.write(line) for new_line in record_list: new.write(" "*8 + new_line + "\n") if flag and line.strip().startwith("backend") flag = False new.write(line) continue if line.strip() and not flag: new.write(line) bk= ‘test.oldboy.org‘ rd="server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000" add(bk,rd) #思路二: s="[11,22,33,44,55]" #字典的表示法为s = ‘{"k1":"v1"}‘,里面元素如果是字符串的话一定要用双引号 print(type(s),s) import json n = json.loads(s) #将一个字符串转换为python的数据类型,[],{} print(type(n),n) import json r = input("input:") dic=json.loads(r) bk=dic[‘beckend‘] rd = "server %s %s weight %d maxconn %d" %(dic[‘record‘][server], dic[‘record‘][server],dic[‘record‘][‘weight‘],dic[‘record‘][‘maxconn‘])
时间: 2024-11-01 01:45:06