5.修改haproxy配置文件
需求:
1、查
输入:www.oldboy.org
获取当前backend下的所有记录 2、新建
输入:
arg = {
'backend': 'www.oldboy.org',
'record':{
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
} 3、删除
输入:
arg = {
'backend': 'www.oldboy.org',
'record':{
'server': '100.1.7.9',
'weight': 20,
'maxconn': 30
}
}
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.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
简易流程图:

# -*- coding:utf-8 -*-
# Author: JACK ZHAO #菜单列表
operation_list = ["Search haproxy.cfg backend information","Add haproxy.cfg backend information",\
"Delete haproy.cfg backend information"]
#状态判断标志
flag_search = 0
flag_add = 0 while True:
flag_search = 0
print(" Haproy configuration file management ".center(50, "#"))
for index,i in enumerate(operation_list): #遍历菜单列表
print([index],i)
choice = input("Please enter your choice[Q:exit]:")
if choice.isdigit(): #判读输入是否为整数
choice = int(choice)
if choice >= 0 and choice <= 2: #判读选择是否在菜单对应的数字内
if choice == 0: #查询backend information
url = input("Please enter URL Address:")
with open("haproxy.cfg","r",encoding="utf-8") as f_search: #打开haproxy.cfg一次性读取到内存中
for line in f_search:
if flag_search == 1:
if "backend" in line:
flag_search = 0
continue
else:
if len(line) <= 1: #不打印空白行
continue
else:
print("\033[32;1m%s\033[0m" % line.strip()) #打印符合条件的行,并去掉前后空格、换行符
continue
if "backend %s"%url in line and "use_backend" not in line :
flag_search = 1
elif choice == 1: #新增backend information
arg = input("Pleas enter backend:")
dic_add = eval('''%s'''%arg) #将输入的字符串转成字典
with open("haproxy.cfg", "a+", encoding="utf-8") as f_add:
f_add.seek(0) #将位置光标移动开始位置,a+模式默认光标在文件最后
for line in f_add:
if dic_add["backend"] in line and "use_backend" not in line: #判读backend 是否已经存在
print("\033[31;1m%s is exists.\033[0m" %dic_add["backend"])
flag_add =1
break
if flag_add == 1:
continue
f_add.write("\nbackend %s\n" %dic_add["backend"])
f_add.write("\t\tserver %s weight %s maxconn %s\n" % (dic_add["record"]["server"],\
dic_add["record"]["weight"],dic_add["record"]["maxconn"]))
elif choice == 2:
arg = input("Pleas enter backend:")
dic_delete = eval('''%s''' % arg) #将输入的字符串转为字典格式
with open("haproxy.cfg", "r", encoding='utf-8') as f_delete:
lines = f_delete.readlines()
with open("haproxy.cfg", "w", encoding='utf-8') as f_delete: #覆盖源文件的方式删除内容
for line in lines:
if "backend %s" %dic_delete["backend"] in line or "server %s weight %s maxconn %s"\
%(dic_delete["record"]["server"],dic_delete["record"]["weight"],dic_delete["record"]["maxconn"]) in line:
continue
f_delete.write(line)
else:
print("It's only going to be 0 to 2.") #必须是菜单对应的数字
continue
elif choice == "Q": #选择退出
exit()
else:
print("It has to be an integer.")
5.修改haproxy配置文件的更多相关文章
- s12-day03-work01 python修改haproxy配置文件(初级版本)
#!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo.song ...
- Python3.5 day3作业二:修改haproxy配置文件。
需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 lo ...
- python基础-修改haproxy配置文件
需要掌握的知识: 1.函数 2.文件处理 3.tag的用法 4.程序的解耦 需求: 1:查询 2:添加 3:删除 4:修改 5:退出 haproxy.conf 配置文件内容: global log 1 ...
- 用python修改haproxy配置文件
需求: 当用户输入域名的时候,显示出来下面的记录 当用户需要输入添加纪录的时候,添加到你需要的那个域名下面 global log 127.0.0.1 local2 daemon maxconn 256 ...
- Python3学习之路~2.10 修改haproxy配置文件
需求: .查 输入:www.oldboy.org 获取当前backend下的所有记录 .新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 's ...
- 修改haproxy配置文件
需求: 1.查 输入:www.oldboy.org 获取当前backend下的所有记录 2.新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ ...
- python基础-4.1 open 打开文件练习:修改haproxy配置文件
1.如何在线上环境优雅的修改配置文件? 配置文件名称ini global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 in ...
- 作业---修改haproxy配置文件
#查询 f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8") ha ...
- python编辑修改haproxy配置文件--文件基础操作
一.需求分析 有查询,删除,添加的功能 查询功能:查询则打印查询内容,如果不存在也要打印相应的信息 删除功能:查询到要删除内容则删除,打印信息. 添加功能:同上. 二.流程图 三.代码实现 本程序主要 ...
随机推荐
- replace into 浅析
转载自:http://blog.itpub.net/22664653/viewspace-1669734/ http://blog.itpub.net/22664653/viewspace-16701 ...
- linux 内存地址空间管理 mm_struct
http://blog.csdn.net/yusiguyuan/article/details/39520933 Linux对于内存的管理涉及到非常多的方面,这篇文章首先从对进程虚拟地址空间的管理说起 ...
- 转:adb操作命令详解及大全
说到 ADB 大家应该都不陌生,即 Android Debug Bridge,Android调试桥,身为 Android 开发的我们,熟练使用 ADB 命令将会大大提升我们的开发效率, ADB 的命令 ...
- python 删除空白
Python能够找出字符串开头和末尾多余的空白.要确保字符串末尾没有空白,可使用方法rstrip() . >>> favorite_language = 'python ' > ...
- SWFUpload 参数详解
属性 类型 默认值 描述 upload_url String 处理上传文件的服务器端页面的url地址,可以是绝对地址,也可以是相对地址,当为相对地址时相对的是当前代码所在的文档地址 preserv ...
- echarts学习笔记(部分angular及ant-design)
1.在项目中修改ng-zorro组件默认样式的一些方法: 类名等 前加::ng-deep: 类名等 前加:root: 类名等 前加:host /deep/: 2.echarts横轴自定义时间粒度 两种 ...
- c++ 11 线程池的简单封装
#include <condition_variable> #include <queue> #include <thread> #include <vect ...
- TSMessages,非HUD风格的iOS提示框(附官方demo BUG修复方案)
优势 先看效果 个人觉得这种提示效果用在UITableView上要比HUD优雅美观,而其他情况下的提示,用HUD比较好 源码简介易懂,用起来也很方便 导入 pod导入相对很简单,主要讲怎么手动导入这个 ...
- hdu_3501_Calculation 2
Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...
- python基础数据类型之字符串操作
1.字符串切片ps:字符串是不可变的对象, 所以任何操作对原字符 是不会有任何影响的 s1 = "python最简洁" print(s1[0]) print(s1[1]) prin ...