Python Day2 (二)

字典的操作:

见连接:

http://www.cnblogs.com/alex3714/articles/5717620.html

 

 

 

第二天作业,修改haproxy配置文件。

程序练习  

程序1: 实现简单的shell sed替换功能

程序2:修改haproxy配置文件 

需求:

1、查
    输入:www.oldboy.org
    获取当前backend下的所有记录

2、新建
    输入:
        arg = {
            'bakend': 'www.oldboy.org',
            'record':{
                'server': '100.1.7.9',
                'weight': 20,
                'maxconn': 30
            }
        }

3、删除
    输入:
        arg = {
            'bakend': 'www.oldboy.org',
            'record':{
                'server': '100.1.7.9',
                'weight': 20,
                'maxconn': 30
            }
        }

需求
需求
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

open_file = 'haproxy.py'
tempfile = '临时.txt'

def file_change():
    os.remove(open_file)
    os.renames(tempfile,open_file)

def search():
    """查询 proxy 配置文件的 server 内容"""
    check = input("请输入要查询的域名:")
    dir = eval(check)
    server_name = dir['backend']
    flag = False
    list = []
    with open(open_file,'r',encoding='utf-8') as file_read:
        for line in file_read:
            if line.startswith("backend") and server_name in line:
                flag = True
                continue
            if line.startswith("backend"):
                break
            if flag:
                list.append(line.strip())
    print("查询结果如下".center(47,'='))
    for i in list:
        print(i)

def add_server():
    """添加的内容默认以字典形式"""
    add_server = input("请输入要添加的内容,示例 { }:")
    #示例添加的字典如:{"backend":"oldboy","record":{"server":'192.168.85.188',"weight":2222,"maxconn":3333}}
    dic = eval(add_server)
    #print(type(dic))
    server_name = dic['backend']
    record = dic['record']
    list = 'server %s %s weight %s maxconn %s\n' % (record['server'],record['server'],record['weight'],record['maxconn'])
    print(list.rjust(70," "))
    with open(open_file,'r+',encoding='utf-8') as f_read,open(tempfile,'w',encoding='utf-8') as f_write:
        for line in f_read:
            if line.startswith("backend") and server_name in line:
                f_write.write(line)
                f_write.write(list.rjust(70," "))
                continue
            f_write.write(line)
        f_read.flush()
    file_change()


def del_server():
    del_server = input("请输入要删除的server信息,实例 { } :")
    dic = eval(del_server)
    server_name = dic['backend']
    server_liset = dic['record']
    server = server_liset["server"]
    with open(open_file,'r',encoding='utf-8') as f_read,open(tempfile,'w',encoding='utf-8') as f_write:
        for line in f_read:
            if line.lstrip().startswith("server") and server in line:
                continue
            f_write.write(line)

    file_change()


def main():
    """  主函数 """
    msg = '''
        1,search:查询
        2,add   :添加
        3.del   :删除'''
    print(msg)
    add = input("请选择上述所对应的序号:")
    if  int(add) == 1:
        search()
    if  int(add) == 2:
        add_server()
    if  int(add) == 3:
        del_server()
if __name__ == '__main__':
    main()
小程序脚本

 

脚本还有待进一步改进。如有建议,请留言哈。

 

以后整理

转载于:https://www.cnblogs.com/Star-Haitian/p/8489690.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值