首页 技术 正文
技术 2022年11月21日
0 收藏 320 点赞 2,892 浏览 3122 个字

文件操作练习:http://www.cnblogs.com/wupeiqi/articles/4950799.html

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 dontlognulllisten stats :8888
stats enable
stats uri /admin
stats auth admin:1234frontend 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 wwwbackend www.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000backend buy.oldboy.org
server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000
复制代码

要求:

输出:
1、获取ha记录
2、增加ha记录
3、删除ha记录num = raw_input('请输入操作序号:')如果用户输入的 1:
read = raw_input('请输入backend:') 如输入:www.oldboy.org
讲配置文件 backend www.oldboy.org 节点下的所有记录获取到,并输入到终端如果用户输入的 2:
read = raw_input('请输入要新加的记录:') 如: {"backend": "test.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 30}}
# 讲read字符串转换成 字典类型
read_dict = json.loads(read) backend_title = read_dict['backend']
# 去配置文件中找到指定的节点:
# backend test.oldboy.org
# 如果已经存在,
# 则在此节点下添加根据用输入构造出的记录,例如:
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
# 如果不存在,
# 则添加backend节点和记录,例如:
backend test.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 # (可选)可以再对节点下记录进行判断是否已经存在如果用户输入的 3:
read = raw_input('请输入要删除的记录:') 如: {"backend": "test.oldboy.org","record":{"server": "100.1.7.9","weight": 20,"maxconn": 30}}
# 讲read字符串转换成 字典类型
read_dict = json.loads(read)
backend_title = read_dict['backend']
#去配置文件中找到指定的节点,并在删除指定记录,如:
backend test.oldboy.org
server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000 # 删除掉 # (可选)如果backend下所有的记录都已经被删除,那么将当前 backend test.oldboy.org 也删除掉。

参考思路:

http://www.cnblogs.com/wupeiqi/articles/5539371.html

#一行一行读
f=open(xxx)
for line in f:
print(line)def fetch(backend):
result=[]
with open("ha.conf",'r',encoding="utf-8") as f:
flag = False
for line in f:
if line.strip().startwith("backend") and line.strip() == "backend "+backend
flag = True
continue
if flag==True and line.strip().startwith("backend"):
flag=False
break
if flag and line.strip():
result.append(line.strip())
return result def add(backend,record):
#思路一:
#先检查记录是否存在
record_list=fetch(backend)
if not record_list:
#backend不存在
with open('ha.conf',r) as old,open("new.conf",'w') as new:
for line in old:
new.write(line)
new.write("backend "+backend +"\n")
new.write(" " * 8 + record + "\n")
else:
#backend存在
#如果记录存在,不用做任何操作,如果不存在,则将记录添加到backend下
if record in record_list:
#记录存在
import shutil
shutil.copy("ha.conf","new.conf")
pass
else:
#backend存在,record不存在
record_list.append(record)
with open('ha.conf',r) as old,open("new.conf",'w') as new:
flag = False
for line in old:
if line.strip().startwith("backend") and line.strip() == "backend "+backend:
flag = True
new.write(line)
for new_line in record_list:
new.write(" "*8 + new_line + "\n")
if flag and line.strip().startwith("backend")
flag = False
new.write(line)
continue
if line.strip() and not flag:
new.write(line)bk= 'test.oldboy.org'
rd="server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000"
add(bk,rd)
#思路二:s="[11,22,33,44,55]" #字典的表示法为s = '{"k1":"v1"}',里面元素如果是字符串的话一定要用双引号
print(type(s),s)
import json
n = json.loads(s) #将一个字符串转换为python的数据类型,[],{}
print(type(n),n)import json
r = input("input:")
dic=json.loads(r)
bk=dic['beckend']
rd = "server %s %s weight %d maxconn %d" %(dic['record'][server],
dic['record'][server],dic['record']['weight'],dic['record']['maxconn'])
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849