haproxy 文件操作,操作属于简单操作,不复杂

 # -*- coding:utf-8 -*-
# LC def search(*args): #查找Haproxy文件中的服务器
list1 = []
with open("haproxyfile","r") as f:
flag = False
for line in f:
if line.strip() == "backend %s" % args: #查找第一个以backend + 输入域名的起头的backend,将相关信息记录list中
flag = True
list1.append(line.strip())
continue #并继续循环,为了将backend下一行执行,即按着flag = True执行
if line.strip().startswith("backend") or line.strip() == "": #查到第二个backend这将flag置于Fasle,如果是空行,也将flag置于Fasle中,(防止文章末尾有多个空行)
flag = False
if flag:
list1.append(line.strip()) #将正确backend的后续信息继续记录至list中
return list1 def delete(string):
dict = eval(string)
backend = dict["backend"]
record = dict["record"]
new_file_list = []
if backend in domain_list(): #判读要删除的域名是否在文件内
with open("haproxyfile","r",encoding="utf-8") as f_read:
for line in f_read:
new_file_list.append(line) #读取文件的每行,将每行写入列表
if line.strip() == "backend %s" %backend: #如果有符合的backend,则将最近的写入列表弹出
new_file_list.pop()
if line.strip() == "server %s weight %s maxconn %s" %(record["server"],record["weight"],record["maxconn"]):#如果有符合的server信息,则将最近的写入列表弹出
new_file_list.pop()
with open("haproxyfile1", "a") as f_write: #将删除后的写入文件中
for line in new_file_list:
f_write.write(line)
else:
print("The Domain not in this file!") def add(string): #增加ha文件配置服务器信息
dict = eval(string)
backend = dict["backend"]
record = dict["record"]
with open("haproxyfile","a") as f_write:
f_write.write("backend %s\n"%backend)
f_write.write("\t\tserver %s %s weight %s maxconn %s"%(record["server"][0],record["server"][1],
record["weight"],
record["maxconn"])) def domain_list(): #将文件中所有的domain都摘出来
backend_name = []
with open("haproxyfile","r") as f_read:
for line in f_read:
if line.startswith("backend"):
domain_name = line.split()[1]
backend_name.append(domain_name)
return backend_name #域名信息查找
domain_search_name = input("Please input the domain your search:")
if domain_search_name in domain_list():
domain_info = search(domain_search_name)
print(domain_info) #域名信息删除 string = "{'backend': 'www.yst.com.cn','record':{'server':'1.1.1.1','weight': 30,'maxconn': 2300}}"
delete(string) #域名信息增加
string = "{'backend': 'www.yst.com.cn','record':{'server':'1.1.1.1','weight': 30,'maxconn': 2300}}"
add(string)
收集backend信息和对应server的信息
 def backend_info():
with open("haproxyfile","r") as f_read:
backend_list = []
dict_all = {}
dict_ser = {}
server_list = []
for line in f_read:
if line.strip().startswith("backend"):
backend_name = line.split()[1]
backend_list.append(backend_name)
server_list = []
if line.strip().startswith("server"):
dict_ser['server'] = line.strip().split()[1]
dict_ser['weight'] = line.strip().split()[3]
dict_ser['maxconn'] = line.strip().split()[5]
server_list.append(dict_ser)
dict_all[backend_name] = server_list
return backend_list,dict_all #返回backend的所有域名信息和域名包含的服务器信息

 

python---haproxy---文件操作的更多相关文章

  1. Python :open文件操作,配合read()使用!

    python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...

  2. Python 常见文件操作的函数示例(转)

    转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...

  3. 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容

     孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...

  4. 孤荷凌寒自学python第三十三天python的文件操作初识

     孤荷凌寒自学python第三十三天python的文件操作初识 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天开始自学python的普通 文件操作部分的内容. 一.python的文件打开 ...

  5. python中文件操作的六种模式及对文件某一行进行修改的方法

    一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...

  6. python中文件操作的其他方法

    前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...

  7. Python常见文件操作的函数示例

    # -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...

  8. python的文件操作及简单的用例

    一.python的文件操作介绍 1.文件操作函数介绍 open() 打开一个文件 语法:open(file, mode='r', buffering=-1, encoding=None, errors ...

  9. python基本文件操作

    python文件操作 python的文件操作相对于java复杂的IO流简单了好多,只要关心文件的读和写就行了 基本的文件操作 要注意的是,当不存在某路径的文件时,w,a模式会自动新建此文件夹,当读模式 ...

  10. [转]python file文件操作--内置对象open

    python file文件操作--内置对象open   说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...

随机推荐

  1. awk:快速入门(简单实用19例+鸟哥书内容)

    awk 用法:awk ' pattern {action} '  变量名 含义  ARGC 命令行变元个数  ARGV 命令行变元数组  FILENAME 当前输入文件名  FNR 当前文件中的记录号 ...

  2. 【一天一道LeetCode】#117. Populating Next Right Pointers in Each Node II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  3. android值得珍藏的6个开源框架技术

    1.volley  项目地址 https://github.com/smanikandan14/Volley-demo JSON,图像等的异步下载: 网络请求的排序(scheduling) 网络请求的 ...

  4. iOS中 MPMoviePlayer 实现视频音频播放 作者:韩俊强

    ios播放视频文件一般使用 MPMoviePlayerViewController 和 MPMoviePlayerController.前者是一个view,后者是个Controller.区别就是 MP ...

  5. pig简单的代码实例:报表统计行业中的点击和曝光量

    注意:pig中用run或者exec 运行脚本.除了cd和ls,其他命令不用.在本代码中用rm和mv命令做例子,容易出错. 另外,pig只有在store或dump时候才会真正加载数据,否则,只是加载代码 ...

  6. 尚学堂马士兵struts2 课堂笔记(四)

    27 结果类型 主要就四种种 dispatch和rediret chain和drdirectaction <package name="resultTypes" namesp ...

  7. React Native调试心得

    在做React Native开发时,少不了的需要对React Native程序进行调试.调试程序是每一位开发者的基本功,高效的调试不仅能提高开发效率,也能降低Bug率.本文将向大家分享React Na ...

  8. mysql进阶(七)limit的用法

    limit是mysql的语法 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条. select *  ...

  9. hive:(group by, having;order by)的使用;group by+多个字段,以及wiki说的group by两种使用限制验证

    hive> select * from app_data_stats_historical where os='1' group by dt limit 100; 出现结果如下: 2014-01 ...

  10. Linux特殊权限分析(第二版)

    SetUID[权限值=4] 问题:为什么普通用户可以修改自己的密码? ll $(which passwd) 1.SetUID:当一个可执行程序/命令具有SetUID 权限,用户执行这个程序时,将以这个 ...