python操作haproxy.cfg文件
需求
1、查
输入:www.oldboy.org
获取当前backend下的所有记录 2、新建
输入:
arg = {
'bakend': 'www.oldboy.org',
'record':{
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
} 3、删除
输入:
arg = {
'bakend': 'www.oldboy.org',
'record':{
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
}
操作haproxy.cfg的haproxy.py文件
#!/usr/bin/evn python
#-*- coding=utf-8 -*-
import json
import os # 根据backend名字查找backend信息
def fetch(backend_name):
line_list = []
flag = False
with open('D:/python/day03/haproxy.cfg','r') as ha:
for line in ha:
if line.startswith("backend %s" % backend_name):
flag = True
continue
if flag and line.strip().startswith('server'):
line_list.append(line.strip())
if line.startswith('backend'):
# flag = False
break return line_list # 添加backend信息
def add(info_dict):
current_title = info_dict['backend']
current_list = 'server %s %s weight %d maxconn %d' % (info_dict['record']['server'],info_dict['record']['server'],info_dict['record']['weight'],info_dict['record']['maxconn'])
fetch_list = fetch(current_title)
# backend是否存在
if fetch_list:
if current_list in fetch_list:
pass
else:
fetch_list.append(current_list)
# 如果根据backend找到了这个backend说明有记录,继续添加server即可 flag = False
has_write = False
with open('haproxy.cfg') as read_obj,open('haproxy.new','w') as write_obj:
for read_line in read_obj:
# 将haproxy.cfg分为上、中、下三部分
# fetch_list为中间部分 # 读取配置文件,写信配置文件
# 读上-->写上
# 将处理完的配置文件fetch_list写入新的配置文件中
# 读下-->写下
if read_line.strip() == 'backend %s' % current_title:
write_obj.write(read_line)
flag = True
continue
if flag and read_line.startswith('backend'):
flag = False
if flag:
for i in fetch_list:
if not has_write:
write_obj.write('%s%s\n' % (' '*8,i))
has_write = True
else:
write_obj.write(read_line)
else:
# 没有这条记录需要添加backend和server
with open('haproxy.cfg') as read_obj,open('haproxy.new','w') as write_obj:
for read_line in read_obj:
write_obj.write(read_line)
write_obj.write('backend %s\n' % current_title)
write_obj.write(' '*8 + current_list)
# 先将haproxy.cfg备份为haproxy.cfg.bak,将新的文件替换为haproxy.cfg
os.rename('haproxy.cfg','haproxy.cfg.bak')
os.rename('haproxy.new','haproxy.cfg') # 删除backend信息函数
def del_backend(info_dict):
current_title = info_dict.get('backend')
fetch_list = fetch(current_title) current_record = 'server %s %s weight %d maxconn %d' % (info_dict['record']['server'],info_dict['record']['server'],info_dict['record']['weight'],info_dict['record']['maxconn']) # 判断是否有这条记录
if fetch_list:
if current_record in fetch_list:
# 将该记录删掉
fetch_list.remove(current_record)
print fetch_list
with open('haproxy.cfg') as read_obj,open('haproxy.new','w') as write_obj:
flag = False
has_write = False
for read_line in read_obj: if read_line.strip() == 'backend %s' % current_title:
write_obj.write(read_line)
flag = True
continue
if flag and read_line.startswith('backend'):
flag = False
if flag:
if not has_write:
for line in fetch_list:
write_obj.write('%s%s\n' % (' '*8,line))
has_write = True
else:
write_obj.write(read_line)
else:
print('no this server record') if os.path.exists('D:/python/day03/haproxy.new'):
os.rename('haproxy.cfg','haproxy.cfg.bak')
os.rename('haproxy.new','haproxy.cfg')
else:
print('no this record') # 需要添加的记录
data = '{"backend": "www.oldboy.org","record":{"server": "100.1.7.168","weight": 20,"maxconn": 30}}'
# 转换为json格式
info_dict = json.loads(data) input_num = raw_input('please input your select : ')
if str.isdigit(input_num):
input_num = int(input_num)
# 1.查找记录 2.增加记录 3.删除记录
if input_num == 1:
pass
elif input_num == 2:
add(info_dict)
elif input_num == 3:
del_backend(info_dict)
haproxy.cfg配置文件
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
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
server 100.1.7.10 100.1.7.10 weight 10 maxconn 2000
backend mysqlserver
server mysql1 10.1.1.110:3306 weight 20 maxconn 300
server mysql2 10.1.1.120:3306 weight 10 maxconn 200
frontend mysql
bind *:3306
mode tcp
log global
default_backend mysqlserver
python操作haproxy.cfg文件的更多相关文章
- Python 操作 MS Excel 文件
利用 Python 对 Excel 文件进行操作需要使用第三方库: openpyxl,可执行 pip install openpyxl 进行安装 1. 导入 openpyxl 模块 导入 openpy ...
- python操作excel表格文件--使用xlrd模块
原文: http://www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html 引言: 实际工作中,可能很多情况下都会用到excel表格,像如果不需 ...
- python操作上级子文件
. └── folder ├── data │ └── data.txt └── test1 └── test2 └── test.py import os '***获取当前目录***'print o ...
- 第三章:Python基础の函数和文件操作实战
本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...
- 关于Haproxy安装和配置:负载配置【haproxy.cfg】问题记录
1. 存放地址: more /etc/haproxy/haproxy.cfg ps -ef | grep haproxy 看看有没有haproxy的进程就是了 或者看看服务器的23306的端口有没有 ...
- python操作文件练习,配置haproxy
在使用python操作文件的时候,特别是对于网络设备,通常操作配置文件,会简化配置量,配置文件加载到内存中,运行时使用的是内存中的配置,内存中配置修改后立即生效,如果不将配置内容保存到硬盘中,则下次重 ...
- python之haproxy配置文件操作(第三天)
作业: 对haproxy配置文件进行操作 要求: 对haproxy配置文件中backend下的server实现增删改查的功能 一.这个程序有二个版本 1. python2.7版本见haproxy_py ...
- Python基础7:文件操作
[ 文件操作] 1 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧 ...
- Python 第三天 文件操作(2)
文件操作 操作文件时,一般需要经历如下步骤: 打开文件 操作文件 一.打开 文件句柄 = file('文件路径', '模式') 注:python中打开文件有两种方式,即:open(...) 和 fi ...
随机推荐
- android 之 Hnadler 、Message 、Looper
Handler定义: 主要接受子线程发送来的数据,并用此数据配合主线程更新UI. 为什么要用Handler? 我们手机当中的很多功能或操作是不能都放在Activity当中的,比如下载文件.处理大量数据 ...
- (转)如何修改maven的默认jdk版本
背景:在maven的配置文件中配置编译的jdk插件,就不需要在eclipse中进行重新的指定了. 问题 1.创建maven项目的时候,jdk版本是1.5版本,而自己安装的是1.7或者1.8版本. 2. ...
- ev的offsetX,pageX,clientX和screenX
event.offsetX.event.offsetY(相对事件发生的具体元素左上角的定位) 鼠标相对于事件源元素(srcElement)的X,Y坐标,只有IE事件有这2个属性,标准事件没有对应的属性 ...
- Date对象常用方法
年月日: var oDate = new Date() //年 oDate.getFullYear(); //月 返回的月份要+1才正常 oDate.getMonth()+1: //日 oDate.g ...
- 我的 $OI$, 退役前写点东西
离 \(NOIp2018\) 还有五天, 总想写点什么 马上退役了啊 是什么时候喜欢上信息技术的呢 记不清了, 很小的时候就喜欢捣鼓关于电脑的东西 当时也不知道有算法这种东西 只是知道有黑客 巨 j8 ...
- 因缺失log4j.properties 配置文件导致flume无法正常启动。
因缺失log4j.properties 配置文件导致flume无法正常启动 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.报错:log4j:WARN No appenders ...
- JavaScript中的栈和堆内存,作用域
1.栈 stack”和“堆 heap” 简单的来讲,stack上分配的内存系统自动释放,heap上分配的内存,系统不释放,哪怕程序退出,那一块内存还是在那里.stack一般是静态 ...
- 转--Python标准库之一句话概括
作者原文链接 想掌握Python标准库,读它的官方文档很重要.本文并非此文档的复制版,而是对每一个库的一句话概括以及它的主要函数,由此用什么库心里就会有数了. 文本处理 string: 提供了字符集: ...
- mysql中sql语句的常用语句
1:提取公共的sql语句: 2:动态添加----sql语句: 代码: <insert id="test1" parameterType="com.floor.sho ...
- Java入门系列(十)Java IO
概述 总体而言,java的读写操作又分为两种:字符流和字节流. 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件. 什么是流? ...