首页 > 代码库 > HA proxy文件的插入,查询、删除,python实现
HA proxy文件的插入,查询、删除,python实现
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Longyi import os def find(address): backend = [] server = [] select = [] count = 0 with open ("haproxy",‘r‘) as obj1: for line in obj1: if line.strip().startswith("backend"): backend.append(line.strip()) if line.strip().startswith("server"): server.append(line.strip()) for i in backend: select.append(i.split()) if select[count][1] == address: break count = count+1 return server[count] def new_insert(data_str): backend = "backend " + data_str["backend"] + "\n" record = "server " + data_str["record"]["server"] + " weight " + data_str["record"]["weight"] + " maxconn " + data_str["record"]["maxconn"] + "\n" new2 = backend + record a = [] i = 0 with open("haproxy", ‘r‘) as obj1_r: for line in obj1_r: a.append(line) str = line.split() if "server" in str: i = a.index(line) a.insert(i + 1, new2) with open("haproxy_new_aaaa", ‘w‘) as obj1_w: for j in a: obj1_w.write(j) #os.rename(‘haproxy‘, ‘haproxy_new_aaaa‘) os.rename(‘haproxy‘, ‘haproxy.bak‘) os.rename(‘haproxy_new_aaaa‘, ‘haproxy‘) os.remove(‘haproxy.bak‘) #os.remove(‘haproxy_new_aaaa‘) return "success" def del_backend(data_str): backend = "backend " + data_str["backend"] + "\n" record = "server " + data_str["record"]["server"] + " weight " + data_str["record"]["weight"] + " maxconn " + data_str["record"]["maxconn"] + "\n" backend_list = backend.split() record_list = record.split() print backend_list print record_list a = [] i = 0 with open("haproxy", ‘r‘) as obj1_r: for line in obj1_r: print line a.append(line) str = line.split() #print str if str == backend_list: a.remove(line) if str == record_list: a.remove(line) with open("haproxy_new_aaaa", ‘w‘) as obj1_w: for j in a: obj1_w.write(j) os.rename(‘haproxy‘, ‘haproxy.bak‘) os.rename(‘haproxy_new_aaaa‘, ‘haproxy‘) os.remove(‘haproxy.bak‘) #os.remove(‘haproxy_new_aaaa‘) return "success" if __name__ == "__main__": print "1.查询记录\n2.添加记录\n3.删除记录\n" select_num = raw_input("please input number:") if select_num == "1": address = raw_input("请输入要查询的地址:") find_list = find(address) print find_list elif select_num == "2": print "plz input like this example:" print ‘{"backend":"ttt.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":"3000"}}‘ data_str = eval(raw_input("please input record>>>")) new_insert_list = new_insert(data_str) if new_insert_list == "success": print "记录插入成功,请查看文件内容" else: print "plz input like this example:" print ‘{"backend":"ttt.oldboy.org","record":{"server":"100.1.7.9","weight":"20","maxconn":"3000"}}‘ data_str = eval(raw_input("please input record>>>")) del_backend_list = del_backend(data_str) if del_backend_list == "success": print "记录删除成功,请查看文件内容"
HA proxy文件的插入,查询、删除,python实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。