Python小程序—修改haproxy配置文件
Posted Ian_Learning
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python小程序—修改haproxy配置文件相关的知识,希望对你有一定的参考价值。
程序2:修改haproxy配置文件
需求:
1 1、查 2 输入:www.oldboy.org 3 获取当前backend下的所有记录 4 5 2、新建 6 输入: 7 arg = { 8 ‘bakend‘: ‘www.oldboy.org‘, 9 ‘record‘:{ 10 ‘server‘: ‘100.1.7.9‘, 11 ‘weight‘: 20, 12 ‘maxconn‘: 30 13 } 14 } 15 16 3、删除 17 输入: 18 arg = { 19 ‘bakend‘: ‘www.oldboy.org‘, 20 ‘record‘:{ 21 ‘server‘: ‘100.1.7.9‘, 22 ‘weight‘: 20, 23 ‘maxconn‘: 30 24 } 25 }
1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms 10 timeout client 50000ms 11 timeout server 50000ms 12 option dontlognull 13 14 listen stats :8888 15 stats enable 16 stats uri /admin 17 stats auth admin:1234 18 19 frontend oldboy.org 20 bind 0.0.0.0:80 21 option httplog 22 option httpclose 23 option forwardfor 24 log global 25 acl www hdr_reg(host) -i www.oldboy.org 26 use_backend www.oldboy.org if www 27 28 backend www.oldboy.org 29 server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
暂时只有查和增功能的代码:
1 #! /usr/bin/env python 2 # -*- coding:utf-8 -*- 3 # Author linux ku 4 #{‘backend‘: ‘www.oldboy.org‘,‘record‘:{ ‘server‘: ‘100.1.7.9‘,‘weight‘: 20,‘maxconn‘: 30}} 5 #{‘backend‘: ‘www.cool2.org‘,‘record‘:{ ‘server‘: ‘100.2.8.9‘,‘weight‘: 30,‘maxconn‘: 70}} 6 def recd_add(): 7 ‘‘‘用来增加数据,暂时没有加入判断输入数据类型的内容‘‘‘ 8 add_inf_tmp = input(‘请输入要增加的内容‘) 9 add_inf = eval(add_inf_tmp) 10 with open(‘G:\学习\Python\learnning\homework3\Record.txt‘, ‘r+‘) as file: #用可以读写的形式打开 11 for line in file: #用来判断是否数据已存在 12 if ‘backend‘ in line: 13 line_ful = "backend {website}\n".format(website=add_inf[‘backend‘]) 14 if line == line_ful: 15 return print(‘The record already exits.‘) 16 file.write(‘\nbackend {backend}\n ‘.format(backend = add_inf[‘backend‘])) 17 file.write(‘\tserver {red1} weight {num1} maxconn {num2}\n‘.format(red1=add_inf[‘record‘][‘server‘], #记得读取是要用这个形式的,不能用数字哦,数字那个是列表 18 num1=add_inf[‘record‘][‘weight‘], 19 num2=add_inf[‘record‘][‘maxconn‘])) 20 return print(‘Adding record successfully. ‘) 21 22 23 def recd_read(): 24 ‘‘‘用来读取数据‘‘‘ 25 read_inf = (input(‘请输入要读取的地址的内容‘)) 26 with open(‘G:\学习\Python\learnning\homework3\Record.txt‘, ‘r‘) as file: #用可以读写的形式打开 27 for line in file: #用来判断是否数据已存在 28 if ‘backend‘ in line: 29 line_ful ="backend {website}\n".format(website=read_inf) 30 if line == line_ful: 31 rd = file.readline() 32 return print(rd) #这里要用print,不然输出不了 33 return ‘The record do not exit. ‘ 34 35 """ 36 def recd_delete(): 37 ‘‘‘用来删除数据‘‘‘ 38 del_inf = eval(input(‘请输入要读取的地址的内容‘)) 39 with open(‘G:\学习\Python\learnning\homework3\Record.txt‘, ‘r+‘) as file: #用可以读写的形式打开 40 for line in file: #用来判断是否数据已存在 41 if ‘backend‘ in line: 42 if del_inf in line: 43 rd = file.readline() 44 return rd 45 return ‘The record do not exit. ‘ 46 """ 47 choice_recd = [‘add‘, ‘delete‘, ‘read‘,‘quit‘] #用来判断输入的内容对不对 48 while True: 49 choice = input("Please input ‘add‘, ‘delete‘, ‘read‘,‘quit‘") 50 if choice in choice_recd: #判断输入的内容对不对 51 if choice == ‘add‘: 52 recd_add() 53 elif choice == ‘read‘: 54 recd_read() 55 elif choice == ‘quit‘: 56 break
以上是关于Python小程序—修改haproxy配置文件的主要内容,如果未能解决你的问题,请参考以下文章