Python基础-文件操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python基础-文件操作相关的知识,希望对你有一定的参考价值。
一、项目需求
1、查询:根据输入指定域名,查看haproxy.conf配置文件内backend的详细信息
2、修改:根据输的域名和backend内的IP地址,修改每条backend的权值和最大连接数
3、增加:根据谁的域名,在该域名的backend下增加一条server记录
4、删除:根据输入的域名,删除该域名下的backend内的一条server记录
二、haproxy.conf的原文件如下:
1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms 10 timeout client 50000ms 11 timeout server 50000ms 12 option dontlognull 13 14 listen stats :8888 15 stats enable 16 stats uri /admin 17 stats auth admin:1234 18 19 frontend oldboy.org 20 bind 0.0.0.0:80 21 option httplog 22 option httpclose 23 option forwardfor 24 log global 25 acl www hdr_reg(host) -i www.oldboy.org 26 use_backend www.oldboy.org if www 27 28 backend www.oldboy1.org 29 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333 30 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000 31 server 2.2.2.4 2.2.2.4 weight 20 maxconn 3000 32 backend www.oldboy2.org 33 server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000 34 backend www.oldboy20.org 35 server 10.10.0.10 10.10.0.10 weight 9999 maxconn 333333333
三、代码如下:
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 def pr(): 5 print("请选择需要的操作:") 6 print("1、查询") 7 print("2、删除") 8 print("3、添加") 9 print("4、修改") 10 11 def query(): 12 while True: 13 flag=False 14 name = input("请输入要查询的域名:").strip() 15 with open("haproxy.conf",encoding="utf8") as f_read: 16 for line in f_read: 17 if line.startswith("backend") and name in line: 18 flag= True 19 continue 20 if line.startswith("backend") and flag: 21 flag=False 22 continue 23 if flag: 24 print(line) 25 if name == "quit": 26 break 27 28 29 def modification(): 30 dic = {"backend": "", "server": "", "weight": "", "maxconn": ""} 31 dic["backend"] = input("请输入需要修改的域名:").strip() 32 dic["server"] = input("请输入需要此域名的ip:").strip() 33 dic["weight"] = input("请输入需要此域名weight修改为:").strip() 34 dic["maxconn"] = input("请输入需要此域名maxconn修改为:").strip() 35 flag=False 36 list = [] 37 with open("haproxy.conf",encoding="utf8") as f_read,open("haproxy_new.conf",mode="w",encoding="utf8") as f_write: 38 for line in f_read: 39 if line.startswith("backend") and dic["backend"] in line: 40 f_write.write(line) 41 flag=True 42 continue 43 if "backend" in line and flag: 44 for i in list: 45 if dic["server"] in i: 46 i = " server " + dic["server"] + " weight " + dic["weight"] + " maxconn " + dic[ 47 ‘maxconn‘] + "\n" 48 f_write.write(i) 49 flag=False 50 if flag: 51 list.append(line) 52 if flag==False: 53 f_write.write(line) 54 55 def add(): 56 dic = {"backend": "", "server": "", "weight": "", "maxconn": ""} 57 dic["backend"] = input("请输入需要添加到的域名:").strip() 58 dic["server"] = input("请输入需要添加的ip:").strip() 59 dic["weight"] = input("请输入需要添加的weight:").strip() 60 dic["maxconn"] = input("请输入需要添加的maxconn:").strip() 61 62 flag=False 63 list=[] 64 with open("haproxy.conf",encoding="utf8") as f_read,open("haproxy_new.conf",mode="w",encoding="utf8") as f_write: 65 for line in f_read: 66 if line.startswith("backend") and dic["backend"] in line: #判断输入的域名的行 67 f_write.write(line) 68 flag=True 69 continue 70 if "backend" in line and flag: #判断下一个backend 71 list.append(" server "+dic["server"]+dic["server"]+ "weight "+dic["weight"]+" maxconn "+dic[‘maxconn‘]+"\n") 72 #到这一行后,把输入要添加的内容插入到列表中,然后循环列表,把所有的内容都添加到新生成的文件中 73 for i in list: 74 f_write.write(i) 75 flag=False 76 if flag: 77 list.append(line) 78 if flag==False: #最先满足的条件是这个,前面的都直接插入到新文件中 79 f_write.write(line) 80 81 def delete(): 82 dic = {"backend": "", "server": "", "weight": "", "maxconn": ""} 83 dic["backend"] = input("请输入需要删除的域名:").strip() 84 dic["server"] = input("请输入此域名的ip:").strip() 85 dic["weight"] = input("请输入此域名的weight:").strip() 86 dic["maxconn"] = input("请输入此域名的maxconn:").strip() 87 flag = False 88 list = [] 89 with open("haproxy.conf", encoding="utf8") as f_read, open("haproxy_new.conf", mode="w", encoding="utf8") as f_write: 90 for line in f_read: 91 if line.startswith("backend") and dic["backend"] in line: #判断输入的域名的行 92 f_write.write(line) 93 flag=True 94 continue 95 if "backend" in line and flag: #判断下一个backend 96 for i in list: 97 if dic["server"] in i and dic["weight"] in i and dic["maxconn"] in i: 98 continue 99 f_write.write(i) 100 flag=False 101 if flag: 102 list.append(line) 103 if flag==False: #最先满足的条件是这个,前面的都直接插入到新文件中 104 f_write.write(line) 105 106 def choice(): 107 pr() 108 num=int(input("请输入选择:").strip()) 109 if num == 1: 110 query() 111 elif num == 2: 112 delete() 113 elif num == 3: 114 add() 115 elif num==4: 116 modification() 117 else: 118 print("输入有误,请重新输入!!") 119 120 choice()
以上是关于Python基础-文件操作的主要内容,如果未能解决你的问题,请参考以下文章