思维还有点乱,撸代码到深夜,先上代码吧.(我是跟着武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修改配置文件的更多相关文章

  1. python作业之修改用户配置文件

    用户的配置文件如下 backend oldboy school school1 age 21 weight 210 qq 550176565 iphone 139987676backend oldgi ...

  2. python作业:HAproxy配置文件操作(第三周)

    一.作业需求: 1. 根据用户输入输出对应的backend下的server信息 2. 可添加backend 和sever信息 3. 可修改backend 和sever信息 4. 可删除backend ...

  3. day3修改配置文件

    有如下配置文件,在指定文件位置添加一条新的记录: global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaul ...

  4. Day3作业:ha_proxy配置文件修改

    不废话,上代码 readme: # Auther:ccorz Mail:ccniubi@163.com Blog:http://www.cnblogs.com/ccorz/ # GitHub:http ...

  5. Python学习day3作业

    days3作业 作业需求 HAproxy配置文件操作 根据用户输入,输出对应的backend下的server信息 可添加backend 和sever信息 可修改backend 和sever信息 可删除 ...

  6. python查询修改配置文件功能

    阅读目录 一.python查询功能代码 1.查询修改配置文件 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 i ...

  7. python读写修改配置文件(ini)

    python 有时候参数需要保存到配置文件中  接下来总结一下 配置文件的读写和修改的操作 代码如下: #!/usr/bin/env python # -*- coding: utf- -*- # 读 ...

  8. python读写增删修改ini配置文件

    一,百度百科 .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的 ...

  9. python笔记 - day3

    python笔记 - day3 参考:http://www.cnblogs.com/wupeiqi/articles/5453708.html set特性: 1.无序 2.不重复 3.可嵌套 函数: ...

随机推荐

  1. cf Magic Numbers

    http://codeforces.com/contest/320/problem/A #include <cstdio> #include <cstring> using n ...

  2. linux vmstat使用说明

    FIELD DESCRIPTION FOR VM MODE Procs r: The number of processes waiting for run time. 表示运行队列,就是说多少个进程 ...

  3. POJ2828---线段树与逆序数&&DUTOJ1210---逆序对构造排列

    来看这样一道问题:http://acm.dlut.edu.cn/problem.php?id=1210 题目大意:对于一个1-n的排列,a1,a2,a3,a4...an我们把满足i < j,ai ...

  4. 【译】Javascript中的数据类型

    这篇文章通过四种方式获取Javascript中的数据类型:通过隐藏的内置[[Class]]属性:通过typeof运算符:通过instanceof运算符:通过函数Array.isArray().我们也会 ...

  5. c语言typedef运用与函数指针

    #include <stdio.h> #include <stdlib.h> #define PINT int * typedef short* PSHORT; //typed ...

  6. 关于<ul><ol><li>的用法

    <ul>:无序列表 <ol>:有序列表 <li>:行. 想要去掉前面的序号和点可以在<ol>或<ul>style中用list-style: ...

  7. Wince修改系统时间问题

           当我们需要修改到系统时间的时候,需要用到下面四个函数:SetLoaclTime,GetLocalTime,SetSystemTime,GetSystemTime.这四个函数是用来修改或者 ...

  8. maven plugin在tomcat 热部署

    前言: 此处的方法适用于tomcat6 和 tomcat7,对于最新的tomcat8还没有进行过測试,有兴趣的同学能够自己測一下. 总共分为五步:         1.在tomcat中配置用户权限,即 ...

  9. 多封装,少开放。强烈建议C++标准添加class之间的注入机制

    近日在改动了一下下引擎代码(为了自己的组件),发现有些接口是仅仅有特定类及其内部函数才去訪问,却不使用友元声明的形式进行数据訪问--当然使用了普通非virtual的形式也就是意味着不建议重载. 故此: ...

  10. IE常见的CSS的BUG(一)

    2011年6月,我毕业了.2012年我接触了CSS,本以为会好过些能赚点钱了,可谁知,Internet Explorer(下称IE),这个前端工程师的噩梦浏览器让我不再那么好过了.各种出现在IE身上的 ...