python作业day3修改配置文件

思维还有点乱,撸代码到深夜,先上代码吧.(我是跟着武sir的思路的)

流程图:

python作业day3修改配置文件


代码(有注释):

 #!/usr/bin/env python
# -*- coding:utf-8 -*-
import json,os
def login():
flag = False
while True:
username = input("Please enter a user name:")
l = open('name_lock.txt','r')
for lline in l.readlines():
lline = lline.strip()
if username == lline:
print("账号被锁")
flag = True
break
if flag == True:
break
#while True:
u = open('name.txt','r')
for uline in u.readlines():
user,password,mony = uline.strip().split(':')
#print(user) if username == user: i = 0
while i < 3:
passwd = input('Please enter a password:')
i +=1 if passwd == password:
print('欢迎%s登陆管理平台' % username)
flag = True
break else:
if i >= 3:
with open('name_lock.txt','a') as l_2:
l_2.write(username + '\n')
exit("试了太多次,将被锁定,请联系管理员")
print('密码输入错误,还有%d次机会' % (3 - i))
break
else:
print("用户输入错误,请重新输入")
if flag == True:
break def chazhao(backend):
cz_list = []
flag = False
with open('ha.txt','r') as cz_1: #打开旧配置文件
for line in cz_1:
cz_line = line.strip()
if cz_line == "backend %s" % backend: #如果读取出的行等于输入的backend
flag = True
continue
if flag == True and cz_line.startswith('backend'): #如果flag=True并且行开头为backend
break
if flag == True and cz_line.startswith('server'): #如果flag= True并且行开头为server
cz_list.append(cz_line)
return cz_list def add_1(dict_info):
backend_title = dict_info.get('backend')
crrent_title = "backend %s" %backend_title
crrent_record = "server %s %s weight %s maxconn %s" %(dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
cz_list = chazhao(backend_title)
if cz_list:
#backend存在
if crrent_record in cz_list: #如果新的数据在旧的配置文件中
pass #不操作
else: #否则,进行以下操作
cz_list.append(crrent_record) #插入数据
#处理完成之后的中间部分
#读取旧配置文件,写入新的配置文件中
#边读边写
#新处理后的配置文件写入新的配置文件中
flag = False
flag_1 = False
with open('ha.txt','r') as ha_1,open('ha_new.txt','w') as ha_2: #打开旧配置文件,打开新配置文件
for line in ha_1:
if line.strip() == crrent_title: #如果读取的行等于输入的backend
flag = True
ha_2.write(line) #将读取的行写入新文件
continue #重新进行循环
if flag == True and line.startswith('backend'): #如果标志位等于True并且此行开头为‘backend’
flag = False #设置标志位为False(因为循环到了下一个‘backend’)
if flag == True and line.startswith('server'): #把已经处理完的数据,写入新配置文件中
if not flag_1:
for nwe_line in cz_list:
temp = ("\t"+nwe_line + "\n")
ha_2.write(temp.expandtabs()) #将新的数据按行写入新配置文件中
flag_1 = True
else:
ha_2.write(line)
else:
#backend不存在,添加记录和backend
with open('ha.txt','r') as ha_1,open('ha_new.txt','w') as ha_2:
for line in ha_1:
ha_2.write(line)
ha_2.write("\n" + crrent_title + "\n")
temp = ("\t"+crrent_record + "\n")
ha_2.write(temp.expandtabs())
os.rename("ha.txt","ha.txt.back") #将旧配置文件备份
os.rename("ha_new.txt","ha.txt") #将新配置文件命名为线上使用的文件名
os.rename("ha.txt.back","ha_new.txt") def delt(dict_info):
backend_title = dict_info.get('backend')
crrent_title = "backend %s" %backend_title
crrent_record = "server %s %s weight %s maxconn %s" %(dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
cz_list = chazhao(backend_title)
if cz_list: #如果列表有数据
if crrent_record in cz_list: #如果数据在列表中
print(cz_list)
del cz_list[cz_list.index(crrent_record)] #删除这个列表中找到的对应数据
print(cz_list)
with open('ha.txt','r') as ha_1,open('ha_new.txt','w') as ha_2:#打开新旧配置文件
flag = False
flag_1 = False
for line in ha_1: #读取行在旧配置文件中
line_strip = line.strip() #将读取的行前后空格删除
if line_strip == crrent_title: #如果删除空格后的行数据等于输入的backend
if len(cz_list)>0: #如果列表中还有数据
ha_2.write(crrent_title + '\n') #将backend写回去(前面删除的时候会把当前backend删除掉)
flag = True
continue #跳出循环
if flag == True and line_strip.startswith('backend'): #如果flag等于True并且行开头为backend
flag = False
if flag == True and line_strip.startswith('server'): #如果flag= True并且行开头为server执行下面步骤
if not flag_1:
for i in cz_list:
temp = ("\t"+ i + "\n")
ha_2.write(temp.expandtabs())
flag_1 = True else:
ha_2.write(line)
else:
return
os.rename("ha.txt","ha.txt.back")
os.rename("ha_new.txt","ha.txt")
os.rename("ha.txt.back","ha_new.txt")
def main():
login()
while True:
print("1.查看 2.添加 3.删除 4.退出")
number = input("请输入序号:") if number == "":
backend_1=input("请输入backend:")
cz = chazhao(backend_1)
print(cz)
continue
elif number == "":
backend_1=input("请输入backend:")
server_1 = input("请输入server:")
weight_1 = input("请输入weight:")
maxconn_1 = input("请输入maxconn:")
s = '{"backend":"%s","record":{"server":"%s","weight":"%s","maxconn":"%s"}}'%(backend_1,server_1,weight_1,maxconn_1)
data_dict = json.loads(s)
add_1(data_dict)
cz_1 = chazhao(backend_1)
print(cz_1)
continue
elif number == "":
backend_1=input("请输入backend:")
cz = chazhao(backend_1)
print(cz)
server_1 = input("请输入server:")
weight_1 = input("请输入weight:")
maxconn_1 = input("请输入maxconn:")
s = '{"backend":"%s","record":{"server":"%s","weight":"%s","maxconn":"%s"}}'%(backend_1,server_1,weight_1,maxconn_1)
data_dict = json.loads(s)
delt(data_dict)
cz_1 = chazhao(backend_1)
print(cz_1)
continue
elif number == "":
exit()
else:
print("输入有误")
continue
if __name__ =='__main__':
main()

配置文件操作

上一篇:PostgreSql中如何kill掉正在执行的sql语句


下一篇:浏览器差异性hack