zoukankan      html  css  js  c++  java
  • 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         }
    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:学习Pythonlearnninghomework3Record.txt', 'r+') as file:  #用可以读写的形式打开
    11         for line in file:  #用来判断是否数据已存在
    12             if 'backend' in line:
    13                 line_ful = "backend {website}
    ".format(website=add_inf['backend'])
    14                 if line == line_ful:
    15                     return print('The record already exits.')
    16         file.write('
    backend {backend}
     '.format(backend = add_inf['backend']))
    17         file.write('	server {red1} weight {num1} maxconn {num2}
    '.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:学习Pythonlearnninghomework3Record.txt', 'r') as file:  #用可以读写的形式打开
    27         for line in file:  #用来判断是否数据已存在
    28             if 'backend' in line:
    29                 line_ful ="backend {website}
    ".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_stupid():
    37     '''用来删除数据,先用个笨办法吧'''
    38     del_inf = eval(input('请输入要删除的地址的内容'))
    39     index_old_file = 0  #两个指针来记录行数变化,来判断输入数据是否正确
    40     index_new_file = 0
    41     with open('G:学习Pythonlearnninghomework3Record.txt', 'r+') as file:  #用可以读写的形式打开
    42         file_tmp = open('G:学习Pythonlearnninghomework3Record_tmp.txt', 'w')
    43         line_ful = "backend {website}
    ".format(website=del_inf['backend'])
    44         for line in file:  #用来新的文件来记录不包含待删除行的内容
    45             if 'backend' not in line:
    46                 file_tmp.write(line)
    47                 index_new_file += 1
    48                 index_old_file += 1
    49             elif line != line_ful:
    50                 file_tmp.write(line)
    51                 index_new_file += 1
    52                 index_old_file += 1
    53             else:
    54                 print('Delete {line}
     	{content}'.format(line = line, content = file.readline()))
    55                 index_old_file += 1
    56     file_tmp.close() #转换模式,不然无法读取
    57     file_tmp = open('G:学习Pythonlearnninghomework3Record_tmp.txt', 'r')
    58     if index_old_file == index_new_file:
    59         file_tmp.close()   #记得关闭
    60         return print('The input content is wrong!')
    61     else:
    62         with open('G:学习Pythonlearnninghomework3Record.txt', 'w') as file:  # 用写模式打开,准备覆盖
    63             for line in file_tmp:
    64                 file.write(line)
    65         file_tmp.close() #记得关闭
    66         return print('The record has been delete. ')
    67 
    68 
    69 choice_recd = ['add', 'delete', 'read','quit']  #用来判断输入的内容对不对
    70 while True:
    71     choice = input("Please input 'add', 'delete', 'read','quit'")
    72     if choice in choice_recd:  #判断输入的内容对不对
    73         if choice == 'add':
    74             recd_add()
    75         elif choice == 'read':
    76             recd_read()
    77         elif choice == 'delete':
    78             recd_delete_stupid()
    79         elif choice == 'quit':
    80             break
    View Code

    http://bbs.csdn.net/topics/340255616

    这个链接里面有点讨论的内容。

    也就是说,文件的修改和读写还可以学习,seek和writelines

  • 相关阅读:
    airodump-ng的使用及显示
    bash shell 遍历一个数组
    cisco 交换机 IOS命令
    apt-key Debian packages密钥管理命令
    find命令的用法
    bash的快捷键
    groff编写man页
    tcpdump软件使用
    vue 项目常见功能(搜索 时间戳转换 过滤器)
    vue2.0 常见功能 (v-for 配置路由 组件渲染)
  • 原文地址:https://www.cnblogs.com/Ian-learning/p/7895004.html
Copyright © 2011-2022 走看看