Day2 - Python基础2作业【文件操作--修改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
  1 #/usr/bin/env python
  2 #-*- coding:utf-8 -*-
  3 # Day2/haproxy.py
  4 
  5 _author_ = 'hepidong'
  6 
  7 # 1、查
  8 #     输入:www.oldboy.org
  9 #     获取当前backend下的所有记录
 10 # 
 11 # 2、新建
 12 #     输入:
 13 #         arg = {
 14 #             'bakend': 'www.oldboy.org',
 15 #             'record':{
 16 #                 'server': '100.1.7.9',
 17 #                 'weight': 20,
 18 #                 'maxconn': 30
 19 #             }
 20 #         }
 21 # 
 22 # 3、删除
 23 #     输入:
 24 #         arg = {
 25 #             'bakend': 'www.oldboy.org',
 26 #             'record':{
 27 #                 'server': '100.1.7.9',
 28 #                 'weight': 20,
 29 #                 'maxconn': 30
 30 #             }
 31 #         }
 32 
 33 def ha_search():
 34     "查找:域名下的所有记录"
 35     hr = open('haproxy', 'r+', encoding='utf-8')
 36     hr.seek(0)
 37     hr_list = hr.read().split('\n')
 38     hs = input('请输入要查找的域名:')
 39     hs = 'backend ' + hs
 40     if hs in hr_list:
 41         for i in hr_list:
 42             if '        server' in i:
 43                 print (i.strip())
 44     else:
 45         print ('输入错误,请重新输入!')
 46     hr.close()
 47 
 48 def ha_add():
 49     "添加记录"
 50     arg = eval(input('请输入要添加的记录:'))
 51     backend = '\nbackend ' + arg['bakend']
 52     server = '\n        server ' + arg['record']['server']
 53     weight = ' weight ' + str(arg['record']['weight'])
 54     maxconn = ' maxconn ' + str(arg['record']['maxconn'])
 55 
 56     ha = open('haproxy', 'a', encoding='utf-8')
 57     ha.write(backend)
 58     ha.write(server + weight + maxconn)
 59     ha.flush()
 60     print ('添加成功')
 61     ha.close()
 62 
 63 
 64 def ha_del():
 65     "删除记录"
 66     hw_del = eval(input('请输入要删除的记录:'))
 67     hr = open('haproxy', 'r+', encoding='utf-8')
 68     hr.seek(0)
 69     hr_list = hr.read().split('\n')
 70     data = ''
 71     for line in hr_list:
 72         if hw_del['record']['server'] in line and str(hw_del['record']['weight']) in line and str(hw_del['record']['maxconn']) in line:
 73             continue
 74         data = data + line + '\n'
 75     hr.close()
 76     with open('haproxy', 'w+', encoding='utf-8') as hw:
 77         hw.write(data)
 78         print ('完成删除!')
 79 
 80 
 81     #hw = open('haproxy', 'w+', encoding='utf-8')
 82 
 83 
 84 def index():
 85 
 86     while True:
 87         print ('1. search\n2. add\n3. delete')
 88 
 89         p = input('请选择要进行的操作(q退出):')
 90         if p == '1':
 91             ha_search()
 92         elif p == '2':
 93             ha_add()
 94         elif p == '3':
 95             ha_del()
 96         elif p == 'q':
 97             exit()
 98         else:
 99             print ('输入错误,请重新输入!')
100 
101 
102 index()

 

posted @ 2017-12-25 14:17  东锅先生丶  阅读(252)  评论(0编辑  收藏  举报