一. 测试程序

 #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-一个文件的增删改查实例的更多相关文章

  1. MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存

    二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...

  2. java对xml文件做增删改查------摘录

    java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...

  3. MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存

    目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...

  4. 【练习】Python第四次:实现对文件的增删改查

    一,实现对文件的增删改查 (一),三级菜单的处理结构及退出技巧:使用TAG标记 tag=True while tag: print('leve1') choice=input("level1 ...

  5. day84-仿照admin实现一个自定义的增删改查组件

    一.admin的使用 app01的admin.py文件: class BookConfig(admin.ModelAdmin): list_display=[] list_display_links= ...

  6. 基于SpringMVC的文件(增删改查)上传、下载、更新、删除

    一.项目背景 摘要:最近一直在忙着项目的事,3个项目过去了,发现有一个共同的业务,那就是附件的处理,附件包括各种文档,当然还有图片等特殊文件,由于时间的关系,每次都是匆匆忙忙的搞定上线,称这项目的空档 ...

  7. java:JSP(JSPWeb.xml的配置,动态和静态导入JSP文件,重定项和请求转发,使用JSP实现数据库的增删改查实例)

    1.JSP的配置: <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  8. BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块

    NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...

  9. Python文件操作-文件的增删改查

    需求:对文件进行增删改查 由于时间原因,本次代码没有增加任何注释,如有疑问,请联系编辑者:闫龙 其实我也是醉了,看着这些个代码,我脑袋也特么大了,没办法,大神说了,不让用新知识,只可以使用学过的,所以 ...

随机推荐

  1. liquibase使用

    1. 创建表 drop database if exists mybatis; create database mybatis; use mybatis; create table mybatis.C ...

  2. 跟我学算法-match-LSTM(向唐老师看齐)

    对于match-lstm,将hi文本与输出的match-lstm(由si,hi,qi)组合重新输入到LSTM网络中,以端对端的操作理念. 参考的博客:https://blog.csdn.net/lad ...

  3. XMLHttpRequest.status 返回服务器状态码

    XMLHttpRequest.status: 1xx-信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应. 100-继续. 101-切换协议. 2xx-成功 ...

  4. 新手C#s.Split(),s.Substring(,)以及读取txt文件中的字符串的学习2018.08.05

    s.split()用于字符串分割,具有多种重载方法,可以通过指定字符或字符串分割原字符串成为字符串数组. //s.Split()用于分割字符串为字符串数组,StringSplitOptions.Rem ...

  5. 54. Spiral Matrix (Graph)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  6. ios Https问题

    HTTPS认证过程:   ① 浏览器发送一个连接请求给安全服务器.   ② 服务器将自己的证书,以及同证书相关的信息发送给客户浏览器.   ③ 客户浏览器检查服务器送过来的证书是否是由自己信赖的 CA ...

  7. C++中public、protected以及private的使用

    相比C语言,C++中通过class/struct来定义既包含数据,又包含行为的结构,从而支持了“对象”.现实世界中,一个人(一个对象)通常 拥有一些资产(数据),并且掌握某些技能(行为),并且这些资产 ...

  8. Master节点部署

    一.部署Kubernetes API服务部署 准备安装包 [root@linux-node1 ~]# cd /usr/local/src/kubernetes [root@linux-node1 ku ...

  9. MySQL 根据年、季度、月、周、日统计数据

    -- 计算每年订单的总价格 select date_format(t.order_time,'%Y') years,sum(t.order_amount) '总价格' from lf_order t ...

  10. sql添加/移除约束

    唯一:ALTER TABLE TableName ADD CONSTRAINT UQ_NickName UNIQUE(NickName) 主键:ALTER TABLE TableName ADD CO ...