python---haproxy---文件操作
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---文件操作的更多相关文章
- Python :open文件操作,配合read()使用!
python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...
- Python 常见文件操作的函数示例(转)
转自:http://www.cnblogs.com/txw1958/archive/2012/03/08/2385540.html # -*-coding:utf8 -*- ''''' Python常 ...
- 孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容
孤荷凌寒自学python第三十五天python的文件操作之针对文件操作的os模块的相关内容 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.打开文件后,要务必记得关闭,所以一般的写法应当 ...
- 孤荷凌寒自学python第三十三天python的文件操作初识
孤荷凌寒自学python第三十三天python的文件操作初识 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天开始自学python的普通 文件操作部分的内容. 一.python的文件打开 ...
- python中文件操作的六种模式及对文件某一行进行修改的方法
一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
- Python常见文件操作的函数示例
# -*-coding:utf8 -*- ''''' Python常见文件操作示例 os.path 模块中的路径名访问函数 分隔 basename() 去掉目录路径, 返回文件名 dirname() ...
- python的文件操作及简单的用例
一.python的文件操作介绍 1.文件操作函数介绍 open() 打开一个文件 语法:open(file, mode='r', buffering=-1, encoding=None, errors ...
- python基本文件操作
python文件操作 python的文件操作相对于java复杂的IO流简单了好多,只要关心文件的读和写就行了 基本的文件操作 要注意的是,当不存在某路径的文件时,w,a模式会自动新建此文件夹,当读模式 ...
- [转]python file文件操作--内置对象open
python file文件操作--内置对象open 说明: 1. 函数功能打开一个文件,返回一个文件读写对象,然后可以对文件进行相应读写操作. 2. file参数表示的需要打开文件的相对路径(当前 ...
随机推荐
- 新手学python(1):解析XML与系统调用
最近需要做一个项目,完成一批音乐的格式转换.由于之前并未学习过python,所以想借此机会学一下.在介绍自己的学习过程之前,先把项目简要描述一下.目前在一台服务器a上有几十万首原始的MP3音乐文件,现 ...
- Synchronize 和 Lock 的区别与用法
一.synchronized和lock的用法区别 (1)synchronized(隐式锁):在需要同步的对象中加入此控制,synchronized可以加在方法上,也可以加在特定代码块中,括号中表示需要 ...
- Java实现二叉树的创建和遍历操作(有更新)
博主强烈建议跳过分割线前面的部分,直接看下文更新的那些即可. 最近在学习二叉树的相关知识,一开始真的是毫无头绪.本来学的是C++二叉树,但苦于编译器老是出故障,于是就转用Java来实现二叉树的操作.但 ...
- emacs24 颜色主题设置
Emacs24 颜色主题设置 在Linux上写程序,永远绕不过的2个东西就是vi和emacs.emacs是早晚要接触的东西.本文就从配置颜色主题(color-theme)开始.用命令:$ sudo a ...
- C++ Primer 有感(标准库set类型)
set容器只是单纯的键的集合,键必须为一.set容器不支持下标操作,而且没有定义maped_type类型.在set容器中,value_type不是pair类型,而是与key_type类型相同的类型. ...
- Struts Chain ClassCastException Aop
我们知道struts的restult type 有很多,但主要就是四种 dispatch,rediret,chain,drdirectaction 要让数据从一个action传到另一个action,就 ...
- 分布式进阶(十一) Docker 常见错误汇总
NO.1 以上添加网桥的命令在Ubuntu14.04中是不可行的.正确的命令如下: brctl addbr br0 ifconfig br0 192.168.1.188 netmask 255.255 ...
- 05_学生管理系统,xml读写,布局的综合应用
最终要做的项目目标: 2.编写Android清单文件AndroidManifest.xml <?xml version="1.0" encoding="utf ...
- Linux0.11启动过程
从开机加电,到执行main函数之前的过程 好吧,这里应该是有执行3个汇编的文件,但是我不太了解.囧 从main函数,到启动OK(即可以响应用户操作了) 这个步骤做了3件事情: 创建进程0,使之具备在主 ...
- wordpress入门基础:wordpress文件系统结构详细介绍
根目录|| wp-admin — wp-content — wp-includes| | |___________________________________ ...