day05.2-一个文件的增删改查实例
一. 测试程序
#INFO.txt源文件内容
global
log 127.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
default
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats: 8888
stats enable
stats uri /admin
stats auth admin:1234
frontend 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 www
backend www.oldboy1.org
server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
#bin.py """
功能:查询文件中是否存在输入标签对应的信息,并打印查询结果
输入:标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
返回:True----文件中存在标签对应的信息 False----文件中不存在标签对应的信息
"""
def fetch(record):
record = eval(record)
tag1 = False
tag2 = False
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
with open("INFO.txt","r") as file:
for item in file:
if item.strip()=="backend "+record["backend"]:
tag1 = True
if tag1 and item.strip()==sub_record:
tag2 = True
break
file.close()
if tag1 and tag2:
return True
else:
return False """
功能:添加标签信息,
如果要添加信息的backend存在,则在backend对应的子标签末尾添加inform对应信息
如果要添加信息的backend不存在,则在文件末尾添加backend以及inform对应信息
输入:要添加的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
"""
def add(record):
record = eval(record)
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
tag1 = False
tag2 = False
with open("INFO.txt","r") as file,\
open("INFO_new.txt","w") as file_new:
for item in file:
if not tag1:
file_new.write(item)
if item.strip()=="backend "+record["backend"]:
tag1 = True
continue
if tag1 and item.startswith(" "):
file_new.write(item)
if tag1 and not item.startswith(" "):
file_new.write(" "*8+sub_record+"\n")
file_new.write(item)
tag2 = True
tag1 = False
if not tag2:
file_new.write("\nbackend "+record["backend"])
file_new.write("\n"+" "*8+sub_record+"\n")
file.close()
file_new.close() """
功能:删除标签信息,
如果要删除的标签信息存在,则删除对于标签,原文件中该标签之后的信息逐行上移一行
如果要删除的标签信息不存在,则显示提示信息
输入:要删除的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
"""
def delete(record):
res = fetch(record)
if not res:
print("要删除的标签信息不存在")
return
else:
tag1 = False
record = eval(record)
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
with open("INFO.txt","r") as file,\
open("INFO_new.txt","w") as file_new:
for item in file:
if not tag1:
file_new.write(item)
if item.strip() == "backend " + record["backend"]:
tag1 = True
if tag1 and item.strip()!=sub_record:
file_new.write(item)
if tag1 and item.strip()==sub_record:
tag1 = False
continue
file.close()
file_new.close() """
功能:修改标签信息,
如果要修改的标签信息存在,则将原文件中该标签信息修改
如果要修改的标签信息不存在,则显示提示信息
输入:要修改的标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
修改后标签信息{"backend":"","inform":{"server":"","weight":"","maxconn":""}}
"""
def modify(recorded,record):
res = fetch(recorded)
if not res:
print("要修改的标签信息不存在")
return
else:
recorded = eval(recorded)
sub_recorded = "server "+str(recorded["inform"]["server"])+" "+str(recorded["inform"]["server"])+" weight "+str(recorded["inform"]["weight"])+" maxconn "+str(recorded["inform"]["maxconn"])
record = eval(record)
sub_record = "server "+str(record["inform"]["server"])+" "+str(record["inform"]["server"])+" weight "+str(record["inform"]["weight"])+" maxconn "+str(record["inform"]["maxconn"])
if recorded["backend"]!=record["backend"]:
print("输入有误")
return
else:
tag1 = False
tag2 = False
with open("INFO.txt", "r") as file, \
open("INFO_new.txt", "w") as file_new:
for item in file:
if item.strip()=="backend "+recorded["backend"]:
tag1 = True
elif tag1 and item.strip()==sub_recorded:
tag2 = True
if tag2:
file_new.write(" "*8+sub_record+"\n")
tag1 = False
tag2 = False
continue
elif not tag2:
file_new.write(item)
file.close()
file_new.close() Menu_msg = {"":fetch,"":add,"":delete,"":modify} #功能函数字典
menu_msg = ["1: fetch","2: add","3: delete","4: modify","5: exit"] while(True):
print("*"*20)
for item in menu_msg:
print(" " + item)
print("*"*20)
opration = input("请输入您的操作:").strip()
if opration=="":
record = input("请输入查询信息").strip()
res=Menu_msg[opration](record)
print(res)
elif opration=="":
record = input("请输入要添加的信息").strip()
Menu_msg[opration](record)
elif opration=="":
record = input("请输入要删除的信息").strip()
Menu_msg[opration](record)
elif opration=="":
recorded = input("请输入要修改的信息").strip()
record = input("请输入修改后信息").strip()
Menu_msg[opration](recorded,record)
elif opration=="":
break
elif opration not in Menu_msg.keys():
continue
二. 功能测试
- 查询
#程序运行过程 ********************
1: fetch
2: add
3: delete
4: modify
5: exit
********************
请输入您的操作:>? 1
请输入查询信息>? {"backend":"www.oldboy1.org","inform":{"server":"100.1000.7.9","weight":20,"maxconn":30}}
True
- 添加
1. 添加子标签
#程序运行过程 ********************
1: fetch
2: add
3: delete
4: modify
5: exit
********************
请输入您的操作:>? 2
请输入要添加的信息>? {"backend":"www.oldboy1.org","inform":{"server":"100.1000.7.9","weight":11,"maxconn":30}}
#INFO_new.txt修改后文件内容 global
log 127.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
default
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats: 8888
stats enable
stats uri /admin
stats auth admin:1234
frontend 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 www
backend www.oldboy1.org
server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
server 100.1000.7.9 100.1000.7.9 weight 11 maxconn 30
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
2. 添加新标签
#程序运行过程 ********************
1: fetch
2: add
3: delete
4: modify
5: exit
********************
请输入您的操作:>? 2
请输入要添加的信息>? {"backend":"www.oldboy3.org","inform":{"server":"100.1000.7.9","weight":20,"maxconn":30}}
#INFO_new.txt修改后文件内容
global
log 127.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
default
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats: 8888
stats enable
stats uri /admin
stats auth admin:1234
frontend 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 www
backend www.oldboy1.org
server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
backend www.oldboy3.org
server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
- 删除
#程序运行过程 ********************
1: fetch
2: add
3: delete
4: modify
5: exit
********************
请输入您的操作:>? 3
请输入要删除的信息>? {"backend":"www.oldboy1.org","inform":{"server":"10.10.0.10","weight":9999,"maxconn":33333333333}}
#INFO_new修改后文件内容
global
log 127.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
default
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats: 8888
stats enable
stats uri /admin
stats auth admin:1234
frontend 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 www
backend www.oldboy1.org
backend www.oldboy1.org
server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
- 修改
#程序运行过程 ********************
1: fetch
2: add
3: delete
4: modify
5: exit
********************
请输入您的操作:>? 4
请输入要修改的信息>? {"backend":"www.oldboy1.org","inform":{"server":"10.10.0.10","weight":9999,"maxconn":33333333333}}
请输入修改后信息>? {"backend":"www.oldboy1.org","inform":{"server":"10.10.0.10","weight":9999,"maxconn":1111}}
#INFO_new.txt文件修改后内容
global
log 127.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
default
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats: 8888
stats enable
stats uri /admin
stats auth admin:1234
frontend 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 www
backend www.oldboy1.org
server 100.1000.7.9 100.1000.7.9 weight 20 maxconn 30
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 1111
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000
backend www.oldboy2.org
server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000
backend www.oldboy20.org
server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
day05.2-一个文件的增删改查实例的更多相关文章
- MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存
二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...
- java对xml文件做增删改查------摘录
java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...
- 【练习】Python第四次:实现对文件的增删改查
一,实现对文件的增删改查 (一),三级菜单的处理结构及退出技巧:使用TAG标记 tag=True while tag: print('leve1') choice=input("level1 ...
- day84-仿照admin实现一个自定义的增删改查组件
一.admin的使用 app01的admin.py文件: class BookConfig(admin.ModelAdmin): list_display=[] list_display_links= ...
- 基于SpringMVC的文件(增删改查)上传、下载、更新、删除
一.项目背景 摘要:最近一直在忙着项目的事,3个项目过去了,发现有一个共同的业务,那就是附件的处理,附件包括各种文档,当然还有图片等特殊文件,由于时间的关系,每次都是匆匆忙忙的搞定上线,称这项目的空档 ...
- java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)
1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块
NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...
- Python文件操作-文件的增删改查
需求:对文件进行增删改查 由于时间原因,本次代码没有增加任何注释,如有疑问,请联系编辑者:闫龙 其实我也是醉了,看着这些个代码,我脑袋也特么大了,没办法,大神说了,不让用新知识,只可以使用学过的,所以 ...
随机推荐
- DBNavigator中把insert变为append
procedure TForm1.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);begin if Button = nbIns ...
- Git----时光穿梭机之工作区和暂存区03
Git和其他版本控制系统SVN的一个不同之处就是有暂存区的概念 先来看看名词解释 工作区(Working Directory) 就是你在我电脑里能看到的目录,比如我的learngittest文件夹就是 ...
- springmvc框架自带的异常处理器SimpleMappingExceptionResolver的使用
使用分三步 1.定义异常类 2.在处理器中的用法 3.在springmvc配置文件中需要加配置
- s 销售视图数据
INSERT,需将公司ID匹对到以下EXCEL表 INSERT,需将公司ID匹对到以下EXCEL表 [Public] ConnectString=host="siebel://10.10.0 ...
- JQuery:选择器、动画、AJAX请求
选择器 1.事件编程与动画效果 一个选择器可以在概念上理解为指针.借助与JQuery库中的选择器$,线程可以获取到界面元素的引用,从而可以调用 ready().click()方法把用户事件和动作方法关 ...
- jmeter 常用函数
${__Random(10000000,19999999,str)};vars.put("msisdn","182"+${__evalVar(str)});
- 表达式SpEL方式的属性注入
-----------------------siwuxie095 表达式 SpEL 方式的属性注入 表达式 SpEL 方式的属性注入是 Spring 3.x 版本后提供的方式 1.编写一个普通类 B ...
- kafka常用运维命令
列出所有topic:bin/kafka-topics.sh --zookeeper localhost:2181 --list说明:其实就是去检查zk上节点的/brokers/topics子节点,打印 ...
- 7-linux-Centos7安装python3并与python2共存
转载自:https://www.cnblogs.com/JahanGu/p/7452527.html linux-Centos7安装python3并与python2共存 1.查看是否已经安装Pyt ...
- 1-vim的复制粘贴
一. http://blog.csdn.net/hk2291976/article/details/42196559 二. "+y"+p---------------------- ...