需求:

1.使python具体增删查的功能。

haproxy的配置文件。

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
import  os,re,sys
from collections import defaultdict,OrderedDict
conf = "haproxy.cfg"
conf_new = "haproxy.cfg.new"
backend_list = []
server_dict= {}
show_dict = {}
backend_name_dict = {}
server_info= []
server_flag = False ###初始化server判断标志位###
with open(conf,'r') as ha: ###打开haproxy配置文件###
for line in ha:
line = line.strip('\n')
if re.match('backend',line): ###匹配配置文件以backend开头的行###
backend_name = re.split('\s+',line)[1]
backend_list.append(backend_name)
server_info1 = []
server_flag = True ###赋值标志位为真,用来与server关联###
elif server_flag and re.match('\s+server',line): ###匹配配置文件以server开头的行###
server_info = re.split('\s+',line)
server_info.remove('')
server_info1.append(server_info)
server_dict['server'] = server_info[1]
server_dict['weight'] = server_info[4]
server_dict['maxconn'] = server_info[5]
backend_name_dict[backend_name] = "name"
backend_name_dict[backend_name] = server_info1
else:
server_flag = False #循环
options_list = ["1.查询backend","2.删除backend","3.新增backend","5.退出"]
while True:
for i in options_list:
print(i)
find = (input("\033[94m请选择操作条目序号:\033[0m"))
if find.isdigit():
find = int((find))
#1是查询
if find == 1:
for i, ele in enumerate(backend_list):
print(i, ele)
find1 = input("请输入您要查询的backend的序号:")
find1 = int(find1)
for j in range(1):
print("==========%s============" %backend_list[find1])
for i in backend_name_dict[backend_list[find1]]:
print("server %s weigt %s maxconn %s" %(i[1],i[3],i[5]))
#2是删除
elif find == 2:
for i, ele in enumerate(backend_list):
print(i, ele)
find3 = int(input("请选择您要删除backend的序号:"))
server_show = []
for i, ele in enumerate(backend_name_dict[backend_list[find3]]):
server_show.append("server %s weight %s maxconn %s" % (ele[1], ele[3], ele[5]))
print(i, "server %s weight %s maxconn %s" % (ele[1], ele[3], ele[5]))
server_show1= str(server_show[0])
print(server_show1)
f = open(conf, "r")
f1 = open(conf_new, "a+")
for i in f:
if server_show1 in i:
i = i.replace(server_show1, "")
f1.write(i)
f1.flush()
f.close()
f1.close()
os.remove(conf)
os.rename(conf_new, conf)
backend_list1 = []
backend_list1.append("backend %s" %(backend_list[find3]))
backend_list2 = str(backend_list1[0])
f = open(conf, "r")
f1 = open(conf_new, "a+")
for i in f:
if backend_list2 in i:
i = i.replace(backend_list2, "")
f1.write(i)
f1.flush()
f.close()
f1.close()
os.remove(conf)
os.rename(conf_new, conf)
print("删除成功!!!")
#3是增加
elif find == 3:
arg = []
backend_name1 = []
backend_list3 = []
input_back = input("\033[94m请输入backend(www.orgboy.org):\033[0m")
arg.append("backend %s" %input_back)
input_server = input("\033[94m请输入server(127.0.0.1):\033[0m")
input_weight = input("\033[94m请输入weight(20):\033[0m")
# arg["weight"] = input_weight
input_maxconn = input("\033[94m请输入maxconn(3000):\033[0m")
arg.append(" server %s weight %s maxconn %s" %(input_server,input_weight,input_maxconn))
backend_name1 = str(arg[0])
f = open(conf, "a+")
f.write("%s \n" %backend_name1)
f.flush()
f.close()
backend_list3 = arg[1]
backend_list3 = str(backend_list3)
f = open(conf, "a+")
f.write("%s \n" %backend_list3)
f.close()
print("添加成功!!!")
#5是退出
elif find == 5:
exit()
else:
print("\033[91m请输入正确的序号\033[0m")
continue

Python3.5 day3作业二:修改haproxy配置文件。的更多相关文章

  1. (转)Python3.5 day3作业二:修改haproxy配置文件

    原文:http://www.cnblogs.com/iwxk/p/6010018.html

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

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

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

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

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

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

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

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

  6. 用python修改haproxy配置文件

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

  7. 修改haproxy配置文件

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

  8. python作业之修改用户配置文件

    用户的配置文件如下 backend oldboy school school1 age 21 weight 210 qq 550176565 iphone 139987676backend oldgi ...

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

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

随机推荐

  1. eclipse逐步调试

    Eclipse 的单步调试 1.设置断点在程序里面放置一个断点,也就是双击需要放置断点的程序左边的栏目上.2.调试(1)点击"打开透视图"按钮,选择调试透视图,则打开调试透视图界面 ...

  2. Selenium Xpath Tutorials - Identifying xpath for element with examples to use in selenium

    Xpath in selenium is close to must required. XPath is element locator and you need to provide xpath ...

  3. 448. Find All Numbers Disappeared in an Array

    https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/ 给出一列数,1 ≤ a[i] ≤ n,n是数组大小,有些 ...

  4. How to Fix GNOME License Not Accepted Issue on CentOS 7

    This post assume that you have just finished the Gnome GUI installation on CentOS 7 by using “yum gr ...

  5. struts2 No result defined for action XXX and result input

    这种错误的原因一般是页面的属性和action里的属性个数.名称.类型不一致造成的 困扰了我一下午的问题,原来是表单中有两个input-text的name属性重复了,然后接受参数的时候就出现了这个错误 ...

  6. Servlet调用过程

    (1)在浏览器输入地址,浏览器先去查找hosts文件,将主机名翻译为ip地址,如果找不到就再去查询dns服务器将主机名翻译成ip地址. (2)浏览器根据ip地址和端口号访问服务器,组织http请求信息 ...

  7. 3ds max录制自定义视频

    要将视频设置成未压缩才能录制出自己设定的大小,其他都是都是特定的,软件默认的一些参数.

  8. HTML入门教程 这里可以免费学习啦

    本文目标 30分钟内让你明白HTML是什么,并对它有一些基本的了解.一旦入门后,你可以从网上找到更多更详细的资料来继续学习. 什么是HTML HTML是英文Hyper Text Mark-up Lan ...

  9. APP开发 Token生成 验证

    准备好协议(HTTP).数据表示方法(JSON).请求数据的方法(REST) 选择一个合适的框架 接口特点汇总: 1.因为是非开放性的,所以所有的接口都是封闭的,只对公司内部的产品有效: 2.因为是非 ...

  10. 恶意软件伪装“正规军”,撕开Booster Cleaner“画皮”下的真相

    经常使用手机浏览器阅读小说的用户都知道,在浏览器页面经常会出现一些推广游戏应用.手机清理应用等应用的弹窗广告.有时出于方便,我们也会选择直接点开这些弹窗广告进行应用下载.但这种行为并不安全,部分恶意应 ...