字典的操作:

见连接:

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()

小程序脚本

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

以后整理

Python Day2 (二)的更多相关文章

  1. python day2:python的基本数据类型及其方法

    目录 python day2 1. 编码转换 2. python的基本数据类型 3. for 迭代遍历 4. 列表list 5. 元组tuple 6. 字典dict 7. 枚举enumerate 8. ...

  2. Python 基础 二

    Python 基础 二 今天对昨天学习的Python基础知识进行总结,学而不思则惘,思而不学则殆! 一.先对昨天学习的三大循环的使用情况进行总结: 1.while循环的本质就是让计算机在满足某一条件的 ...

  3. 初学Python(二)——数组

    初学Python(二)——数组 初学Python,主要整理一些学习到的知识点,这次是数组. # -*- coding:utf-8 -*- list = [2.0,3.0,4.0] #计算list长度 ...

  4. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

  5. 有关python下二维码识别用法及识别率对比分析

    最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...

  6. PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。

    Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...

  7. 从Scratch到Python——Python生成二维码

    # Python利用pyqrcode模块生成二维码 import pyqrcode import sys number = pyqrcode.create('从Scratch到Python--Pyth ...

  8. 用python生成二维码

    Python生成二维码,可以使用qrcode模块, github地址 我是搬运工 首先安装, 因为打算生成好再展示出来,所以用到Pillow模块 pip install qrcode pip inst ...

  9. Python 实现二维码生成和识别

    今天突然想给自己自己做个头像,然后还是二维码的形式,这样只要扫一扫就可以访问我的主页.然后就开始自己的苦逼之路... 其实实现二维码java,c#,C++等都可以实现:由于自己正在学python,所以 ...

  10. Python - 模块(二)

    目录 Python - 模块(二) re re下面的方法 logging os Python - 模块(二) re re模块提供了正则表达式的相关操作 主要字符介绍: . 通配符,除了换行符之外的任意 ...

随机推荐

  1. linux查询日志常用命令,经常更新

    1.grep命令 grep -c "查询内容" filename    ------c,是小写,可以知道你要查询的内容在这个文件中是否存在 grep -C 10 "查询内 ...

  2. 通过Docker发布RestAPI遇到的种种问题

    目标:发布一个分词API 问题1:Docker外无法访问API 原因: Docker映射的地址是0.0.0.0:8888端口,而flask启动的时候默认地址是127.0.0.1:5000,需要手动配置 ...

  3. [Oracle维护工程师手记]为什么flashback 的时候既需要 flashback log ,又需要 archive log?

    为什么flashback 的时候既需要 flashback log ,又需要 archive log 呢? 如果数据库的活动不是很频繁,可以看到,其flashback log 是比较小的.那么是通过怎 ...

  4. 微信小程序开发 (资料汇总,谁还没被坑过?希望助你绕过一些坑)

    最近帮人家做一个微信小程序,刚好想熟悉一下.由于牵扯到多用户使用系统,以及数据共享,所以自然架构选择了,客户端和服务器的方式. 后台服务器是windows server,后台程序是.Net  WebA ...

  5. 分治FFT的三种含义

    分治FFT是几个算法的统称.它们之间并无关联. 分治多项式乘法 问题如求\(\prod_{i=1}^na_ix+b\). 若挨个乘复杂度为\(O(n^2\log n)\),可分治做这件事,复杂度为\( ...

  6. 原生Ajax XMLHttpRequest对象

    一.Ajax请求 - 现在常见的前后端分离项目中,一般都是服务器返回静态页面后浏览器加载完页面,运行script中的js代码,通过ajax向后端api发送异步请求获取数据,然后调用回调函数,将数据添加 ...

  7. Yesterday when I was young

    Somehow, it seems the love I knew was always the most destructive kind 不知为何,我经历的爱情总是最具毁灭性的的那种 Yester ...

  8. window.onload 与 $(document).ready() 的区别

    以浏览器装载文档为例,在页面加载完毕后,浏览器会通过 JavaScript 为 DOM 元素添加事件.在常规的 JavaScript 代码中,通常使用 window.onload 方法 ,而在 jQu ...

  9. centos7之sed和awk常用

    sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令 ...

  10. SELECT list is not in GROUP BY clause and contains nonaggregated column

    报错如下: Expression # of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘sss.m ...