需求:

 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配置文件的更多相关文章

  1. s12-day03-work01 python修改haproxy配置文件(初级版本)

    #!/usr/local/env python3 ''' Author:@南非波波 Blog:http://www.cnblogs.com/songqingbo/ E-mail:qingbo.song ...

  2. Python3.5 day3作业二:修改haproxy配置文件。

    需求: 1.使python具体增删查的功能. haproxy的配置文件. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 lo ...

  3. python基础-修改haproxy配置文件

    需要掌握的知识: 1.函数 2.文件处理 3.tag的用法 4.程序的解耦 需求: 1:查询 2:添加 3:删除 4:修改 5:退出 haproxy.conf 配置文件内容: global log 1 ...

  4. 用python修改haproxy配置文件

    需求: 当用户输入域名的时候,显示出来下面的记录 当用户需要输入添加纪录的时候,添加到你需要的那个域名下面 global log 127.0.0.1 local2 daemon maxconn 256 ...

  5. Python3学习之路~2.10 修改haproxy配置文件

    需求: .查 输入:www.oldboy.org 获取当前backend下的所有记录 .新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ 's ...

  6. 修改haproxy配置文件

    需求: 1.查 输入:www.oldboy.org 获取当前backend下的所有记录 2.新建 输入: arg = { 'bakend': 'www.oldboy.org', 'record':{ ...

  7. python基础-4.1 open 打开文件练习:修改haproxy配置文件

    1.如何在线上环境优雅的修改配置文件? 配置文件名称ini global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 in ...

  8. 作业---修改haproxy配置文件

    #查询 f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8") ha ...

  9. python编辑修改haproxy配置文件--文件基础操作

    一.需求分析 有查询,删除,添加的功能 查询功能:查询则打印查询内容,如果不存在也要打印相应的信息 删除功能:查询到要删除内容则删除,打印信息. 添加功能:同上. 二.流程图 三.代码实现 本程序主要 ...

随机推荐

  1. Poj(1182),种类并查集

    题目链接:http://poj.org/problem?id=1182 再次熟练种类并查集,又积累点经验,和技巧,rank 0 2 1 先计算father[x] ,再更新rank[x]; #inclu ...

  2. 解方程求PH值,POJ(2006)

    题目链接:http://poj.org/problem?id=2006 解题报告: 题意看了半天,没看懂,再加上化学没学好,更加让我头痛. 假设1L溶解了x摩尔的酸:ka=m*x*nx/ori-x; ...

  3. 20145238-荆玉茗 《Java程序设计》第9周学习总结

    20145238第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 ...

  4. 在matlab中查看变量的数据类型

    >> x = x = >> class(x) ans = double

  5. nuget打包

    [1.创建.nuspec文件] 在.csproj目录下运行命令 nuget spec [2.生成包nupkg] 在.csproj目录下运行命令 nuget pack xxxx.csproj [推送命令 ...

  6. window环境下安装node.js

    在使用sublime text 3 过程中,node.js装了好几次都没有成功,今天终于成功了,现将安装过程整理一下. 安装过程中主要参考了以下代码: 第一,下载文件 https://nodejs.o ...

  7. JavaSE 面试题总结

    一. JavaSE 4 1. 面向对象的特征有哪些方面 4 2. String是最基本的数据类型吗? 4 3. super()与this()的区别? 4 4. JAVA的事件委托机制和垃圾回收机制 4 ...

  8. 如何把设计图自动转换为iOS代码? 在线等,挺急的!

    这是一篇可能略显枯燥的技术深度讨论与实践文章.如何把设计图自动转换为对应的iOS代码?作为一个 iOS开发爱好者,这是我很感兴趣的一个话题.最近也确实有了些许灵感,也确实取得了一点小成果,和大家分享一 ...

  9. C#如何使用异步编程【BeginInvoke/EndInvoke】

    怎么使用异步,就是用委托进行处理,如果委托对象在调用列表中只有一个方法,它就可以异步执行这个方法.委托类有两个方法,叫做BeginInvoke和EndInvoke,它们是用来异步执行使用. 异步有三种 ...

  10. Linux中用户与用户组管理

    1.基础知识 Linux作为一种多用户的操作系统(服务器系统),允许多个用户同时登陆到系统上,并响应每个用户的请求. 任何需要使用操作系统的用户,都需要一个系统账号,账号分为:管理员账号与普通用户账号 ...