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配置文件--文件基础操作
一.需求分析 有查询,删除,添加的功能 查询功能:查询则打印查询内容,如果不存在也要打印相应的信息 删除功能:查询到要删除内容则删除,打印信息. 添加功能:同上. 二.流程图 三.代码实现 本程序主要 ...
随机推荐
- 用命令关键字(Cmdlet Keyworlds)编写面向管道的脚本
使用begin process和end关键字 把你的脚本分成 初始化 处理和清楚几个区域
- Better exception message for missing @RequestBody method parameter
https://jira.spring.io/browse/SPR-12888 Description When I use @RequestBody on one of my controllers ...
- OpenACC例子
timeinfo1.c代码 #include<stdio.h> #define N 100 int main() { int A[N]; #pragma acc kernels { ; i ...
- ThinkPHP:create()方法有什么用呢?
1.create方法可以对POST提交的数据进行处理(通过表中的字段名称与表单提交的名称对应关系自动封装数据实例),例如user表中有一个字段名叫"username",如果表单中有 ...
- 基于Jquery的原生态dialog弹出窗口-zapWindow
看到boss系统搓B的填出窗口,不忍直视,坚决的换掉! 采用zapwindow(来源不清楚了,总之是前人留下的),做了修改,当前支持三类弹出类型: 1. 指定url 2. 自定义html 3. 指定D ...
- CocoaAsyncSocket使用
代理的.h文件 #import <Foundation/Foundation.h> #import "GCDAsyncSocket.h" typedef void(^S ...
- C#+Winform开发窗体程序
学习笔记 第一章:winform基础 一.概述 1.Windows Form(简称WinForm) 是微软.NET平台下用于开发"图形界面"应用程序的组件. 2.C/S架构 客户机 ...
- C# Newtonsoft.Json 解析多嵌套json 进行反序列化
[ { ", "time": "2016-09-09 12:23:33", ", "freeShipping": tru ...
- SQLSERVER 数据库恢复挂起的解决办法
如果你的数据库还处于挂起状态,请把我下面代码的test改为你的库名,然后执行完,刷新就正常了: USE masterGOALTER DATABASE test SET SINGLE_USERGOALT ...
- oauth2.0协议接口-第一篇-api逻辑
开放平台是支持OAuth2.0和RESTful协议的资源分享平台,经过授权的合作伙伴可以读取和写入资讯.用户.文件.数据库等资源. 1.创建数据库表结构 CMSSyncClient(数据同步客户端) ...