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()
小程序脚本
脚本还有待进一步改进。如有建议,请留言哈。
以后整理
Python Day2 (二)的更多相关文章
- python day2:python的基本数据类型及其方法
目录 python day2 1. 编码转换 2. python的基本数据类型 3. for 迭代遍历 4. 列表list 5. 元组tuple 6. 字典dict 7. 枚举enumerate 8. ...
- Python 基础 二
Python 基础 二 今天对昨天学习的Python基础知识进行总结,学而不思则惘,思而不学则殆! 一.先对昨天学习的三大循环的使用情况进行总结: 1.while循环的本质就是让计算机在满足某一条件的 ...
- 初学Python(二)——数组
初学Python(二)——数组 初学Python,主要整理一些学习到的知识点,这次是数组. # -*- coding:utf-8 -*- list = [2.0,3.0,4.0] #计算list长度 ...
- Python学习二:词典基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...
- 有关python下二维码识别用法及识别率对比分析
最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...
- PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。
Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...
- 从Scratch到Python——Python生成二维码
# Python利用pyqrcode模块生成二维码 import pyqrcode import sys number = pyqrcode.create('从Scratch到Python--Pyth ...
- 用python生成二维码
Python生成二维码,可以使用qrcode模块, github地址 我是搬运工 首先安装, 因为打算生成好再展示出来,所以用到Pillow模块 pip install qrcode pip inst ...
- Python 实现二维码生成和识别
今天突然想给自己自己做个头像,然后还是二维码的形式,这样只要扫一扫就可以访问我的主页.然后就开始自己的苦逼之路... 其实实现二维码java,c#,C++等都可以实现:由于自己正在学python,所以 ...
- Python - 模块(二)
目录 Python - 模块(二) re re下面的方法 logging os Python - 模块(二) re re模块提供了正则表达式的相关操作 主要字符介绍: . 通配符,除了换行符之外的任意 ...
随机推荐
- PHP中生成UUID
一.什么是UUID 简单的说UUID就是一串全球唯一的(16进制)数字串. UUID的全拼为“Universally Unique Identifier”,可以译为“通用唯一识别码”.UUID由开源软 ...
- 用JS解决url地址中参数乱码的问题
var url = window.location.herf;//获取url地址 var obj = {}; //最后输出的对象 var reg = /\?/; //要匹配的正则表达式 if(url. ...
- Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist
当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...
- Tutorial 01_熟悉常用的Linux操作和Hadoop操作
(一)熟悉常用的Linux 操作cd 命令:切换目录 (1) 切换到目录“/usr/local” (2) 切换到当前目录的上一级目录 (3) 切换到当前登录Linux 系统的用户的自己的主文件夹 ...
- MacOS搭建本地服务器
MacOS搭建本地服务器 一,需求分析 1.1,开发app(ios android)时通常需往app中切入web页面,直接导入不行,故需搭建本地的测试网站服务,通过IP嵌入访问页面. 1.2,开发小程 ...
- 深入理解AMQP协议
深入理解AMQP协议 2018年10月22日 12:32:16 一剑何风情 阅读数:1941 文章目录 一.AMQP 是什么 二.AMQP模型 工作过程 深入理解 三.Exchange交换机 默认 ...
- kubernetes 报错汇总
一. pod的报错: 1. pod的容器无法启动报错: 报错信息: Normal SandboxChanged 4m9s (x12 over 5m18s) kubelet, k8sn1 Pod san ...
- Codeforces Round #542 [Alex Lopashev Thanks-Round] (Div. 2)
A. Be Positive 题意:给出一个数组 每个树去除以d(d!=0)使得数组中大于0的数 大于ceil(n/2) 求任意d 思路:数据小 直接暴力就完事了 #include<bits/s ...
- centos7关闭ipv6
方法一: 编辑文件 /etc/sysctl.conf ,新增以下两行 net.ipv6.conf.all.disable_ipv6 = net.ipv6.conf.default.disable_ip ...
- 修改已有git仓库的远程仓库指向
$ git remote set-url origin git@github.com:test/thinkphp.git 或者 $ git config -e 直接编辑其中origin的url就行了, ...