import os

def fetch(data):
# print('\033[1;43m这是查询功能\033[0m')
# print('\033[1;43m用户数据是\033[0m',data)
backend_data='backend %s'%data
with open('website.conf','r',encoding='utf-8') as read_f:
tag = False
ret = []
for read_line in read_f:
if read_line.strip()==backend_data:
tag=True
continue
if tag and read_line.startswith('backend'):
break
if tag:
print(read_line,end='')
ret.append(read_line)
return ret def add():
pass def change(data):
print('这是修改功能')
backend= data[0]['backend']
backend_data='backend %s'%backend
old_sever_record='%s sever %s weight %s\n'%(' '*8,data[0]['record']['sever'],
data[0]['record']['weight'])
new_sever_record='%s sever %s weight %s\n'%(' '*8,data[1]['record']['sever'],
data[1]['record']['weight']) res=fetch(backend)
print('change函数',res) # if not res or old_sever_record not in res:
# return '你要修改的记录不存在'
# else:
index=res.index(old_sever_record)
res[index]=new_sever_record
res.insert(0,'%s\n'%backend_data)
with open('website.conf','r') as read_f,\
open('website_new.conf','w') as write_f:
tag=False
has_wirte=False
for read_line in read_f:
if read_line.strip()==backend_data:
tag=True
continue
if tag and read_line.startswith('backend'):
tag=False
if not tag:
write_f.write(read_line)
else:
if not has_wirte:
for record in res:
write_f.write(record) def delete():
pass if __name__=='__main__':
msg='''
1.查询
2.添加
3.修改
4.删除
5.退出
'''
msg_dic={
'':fetch,
'':add,
'':change,
'':delete
} while True:
print(msg)
choice=input('请输入你的选项:').strip()
if not choice:continue
if choice==5:break
data=input('请输入你的数据:'.strip())
if choice!='':
data=eval(data)
res=msg_dic[choice](data)
print(res) # [{'backend':'www.newboy2.org','record':{'sever':'1.1.1.8','weight':80}},{'backend':'www.newboy2.org','record':{'sever':'1.0.0.8','weight':100}}]

python练习题_04的更多相关文章

  1. Python练习题 028:求3*3矩阵对角线数字之和

    [Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...

  2. Python练习题 027:对10个数字进行排序

    [Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...

  3. Python练习题 026:求100以内的素数

    [Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...

  4. Python练习题 025:判断回文数

    [Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. ---------------------------------------- ...

  5. Python练习题 024:求位数及逆序打印

    [Python练习题 024] 给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. ---------------------------------------------- ...

  6. Python练习题 004:判断某日期是该年的第几天

    [Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...

  7. Python练习题-1.使用匿名函数对1~1000求和,代码力求简洁。

    Python 练习 标签(空格分隔): Python Python练习题 Python知识点 一.使用匿名函数对1~1000求和,代码力求简洁. 答案: In [1]: from functools ...

  8. PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。

    Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...

  9. python 基础 2.8 python练习题

    python 练习题:   #/usr/bin/python #coding=utf-8 #@Time   :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...

随机推荐

  1. Python爬虫初学者学习笔记(带注释)

    一,安装编程工具并进入编程界面 首先去https://www.continuum.io/downloads/网站下载Anaconda工具并安装;打开cmd,输入jupyter notebook并回车( ...

  2. 两台Linux服务器之间复制文件

    一.scp 1.简介 scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的 2.软件安装 ...

  3. python selenium-webdriver 执行js (八)

    使用python selenium 做自动化测试的时候其实我基本没有使用过执行js,只是在后来使用selenium来爬虫的时候使用到了,所以我在这里也就拿出来简单的介绍一下如何执行js. execut ...

  4. SoapUI工具做get请求和post请求接口测试

    转载自:https://www.cnblogs.com/hong-fithing/p/7617366.html 此篇主要介绍SoapUI工具做常用的两种请求接口测试,分别是get请求和post请求. ...

  5. 在CentOS 7上安装Nginx

    本教程中的步骤要求用户拥有root权限 第一步 - 添加Nginx存储库要添加CentOS 7 EPEL仓库,请打开终端并使用以下命令: sudo yum install epel-release第二 ...

  6. Hello 2019

    2019也要创造价值! 热爱生活!

  7. Elisp 中变量赋值函数 set 与 setq 辨析

    在 Elisp 中,为变量赋值的函数有 set 与 setq,但是,两者存在很大的差异. 使用 set 赋值: 如果我们想为变量 flowers 赋值为一个 列表 '(rose violet dais ...

  8. 「Mobile Testing Summit China 2017」第三届中国移动互联网测试开发大会-讲师征集

    时至北京盛夏,一场由 TesterHome 主办的关于移动互联网测试技术的盛会正在紧锣密鼓的筹备中.只要你关注软件质量,热爱测试,期待学习,都欢迎你加入这次移动测试技术大会中和我们一起分享经验.探讨话 ...

  9. Java可视化日历(Date类、DATe Format类、Calendar类综合运用),开发可视化日历小程序

    Java时间日期类综合运用,开发可视化日历小程序 由键盘输入指定格式的日期,打印这个月的日历 1.代码 import java.text.DateFormat; import java.text.Pa ...

  10. [UE4]Drop,扔物品

    一.Drop只要将Grab做的操作反着这一遍就可以:开启模拟物理.取消附加(使用:DetachFromActor方法,同样的rule也需要使用Keep World) 二.