python作业day3修改配置文件
思维还有点乱,撸代码到深夜,先上代码吧.(我是跟着武sir的思路的)
流程图:

代码(有注释):
#!/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()
配置文件操作
python作业day3修改配置文件的更多相关文章
- python作业之修改用户配置文件
用户的配置文件如下 backend oldboy school school1 age 21 weight 210 qq 550176565 iphone 139987676backend oldgi ...
- python作业:HAproxy配置文件操作(第三周)
一.作业需求: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend ...
- day3修改配置文件
有如下配置文件,在指定文件位置添加一条新的记录: global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaul ...
- Day3作业:ha_proxy配置文件修改
不废话,上代码 readme: # Auther:ccorz Mail:ccniubi@163.com Blog:http://www.cnblogs.com/ccorz/ # GitHub:http ...
- Python学习day3作业
days3作业 作业需求 HAproxy配置文件操作 根据用户输入,输出对应的backend下的server信息 可添加backend 和sever信息 可修改backend 和sever信息 可删除 ...
- python查询修改配置文件功能
阅读目录 一.python查询功能代码 1.查询修改配置文件 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 i ...
- python读写修改配置文件(ini)
python 有时候参数需要保存到配置文件中 接下来总结一下 配置文件的读写和修改的操作 代码如下: #!/usr/bin/env python # -*- coding: utf- -*- # 读 ...
- python读写增删修改ini配置文件
一,百度百科 .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的 ...
- python笔记 - day3
python笔记 - day3 参考:http://www.cnblogs.com/wupeiqi/articles/5453708.html set特性: 1.无序 2.不重复 3.可嵌套 函数: ...
随机推荐
- QT 串口通信 数据16进制发送
在QT中进行串口通信时,很多情况要用到发送16进制的数据.从网上找来了一段代码测试能用: static QByteArray QString2Hex(QString str) { QByteArray ...
- 这样就算会了PHP么?-5
汇集一点关于数据深入一些的几个函数,去重,弹出,加入,查找.... <?php $str = "时装,休闲,职业装"; $strs = explode(",&quo ...
- to_char函数引发的不走索引
SQL> conn cowork_czsh/cowork_czsh Connected. SQL> set linesize 200 SQL> set pagesize 200 SQ ...
- 【转】android 5.0 64bit系统加载库文件失败问题浅析
原文网址:http://blog.csdn.net/andrewblog/article/details/43601303 最近公司的一个项目使用android 5.0 64 bit平台,相对以前版本 ...
- Oracle中对列加密的方法
Oracle中对列加密的方法 2011-12-22 17:21:13 分类: Linux Oracle支持多种列加密方式: 1,透明数据加密(TDE):create table encrypt_col ...
- QQ能上,但是网页打不开的解决办法
QQ能上,但是网页打不开,解决办法是:netsh winsock reset
- MergeSort 归并排序
实现: 二路归并 public class TestMergeSort { public int[] mergeSortArray(int[] arr, int left, int right){ i ...
- LinearLayout 和 RelativeLayout
共有属性: java代码中通过btn1关联次控件 android:id="@+id/btn1" 控件宽度 android:layout_width="80px" ...
- [Node.js] Using ES6 and beyond with Node.js
If you're used to using all the latest ES6+ hotness on the front end via Babel, working in Node.js c ...
- Citrix 服务器虚拟化之十 Xenserver高可用性HA
Citrix 服务器虚拟化之十 Xenserver高可用性HA HA是一套全自动功能设计,规划.它可以安全地恢复出现问题的XenServe 主机.例如物理破坏网络或主机的硬件故障,HA可确保无需任何人 ...