1. 程式人生 > >Python小程序—修改haproxy配置文件

Python小程序—修改haproxy配置文件

format not num org list for choice ces div

程序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 }
View Code 技術分享圖片
 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
View Code

暫時只有查和增功能的代碼:

技術分享圖片
 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
View Code

Python小程序—修改haproxy配置文件