python 配置文件修改_PYTHON 修改配置文件

需要掌握的知识:

1、函数

2、文件处理

3、tag的用法

4、程序的解耦

需求:

1:查询

2:添加

3:删除

4:修改

5:退出

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 33333333333

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 33333333333

一、优化后的代码:

1 #!/usr/bin/env python

2 # -*- coding:utf-8 -*-

3 #Author: nulige

4

5

6 import os

7 def file_handle(filename,backend_data,record_list=None,type='fetch'): #type:fetch append change

8 new_file=filename+'_new'

9 bak_file=filename+'_bak'

10 if type == 'fetch':

11 r_list = []

12 with open(filename, 'r') as f:

13 tag = False

14 for line in f:

15 if line.strip() == backend_data:

16 tag = True

17 continue

18 if tag and line.startswith('backend'):

19 break

20 if tag and line:

21 r_list.append(line.strip())

22 for line in r_list:

23 print(line)

24 return r_list

25 elif type == 'append':

26 with open(filename, 'r') as read_file, \

27 open(new_file, 'w') as write_file:

28 for r_line in read_file:

29 write_file.write(r_line)

30

31 for new_line in record_list:

32 if new_line.startswith('backend'):

33 write_file.write(new_line + '\n')

34 else:

35 write_file.write("%s%s\n" % (' ' * 8, new_line))

36 os.rename(filename, bak_file)

37 os.rename(new_file, filename)

38 os.remove(bak_file)

39 elif type == 'change':

40 with open(filename, 'r') as read_file, \

41 open(new_file, 'w') as write_file:

42 tag=False

43 has_write=False

44 for r_line in read_file:

45 if r_line.strip() == backend_data:

46 tag=True

47 continue

48 if tag and r_line.startswith('backend'):

49 tag=False

50 if not tag:

51 write_file.write(r_line)

52 else:

53 if not has_write:

54 for new_line in record_list:

55 if new_line.startswith('backend'):

56 write_file.write(new_line+'\n')

57 else:

58 write_file.write('%s%s\n' %(' '*8,new_line))

59 has_write=True

60 os.rename(filename, bak_file)

61 os.rename(new_file, filename)

62 os.remove(bak_file)

63

64

65 def fetch(data):

66 backend_data="backend %s" %data

67 return file_handle('haproxy.conf',backend_data,type='fetch')

68

69 def add(data):

70 backend=data['backend']

71 record_list=fetch(backend)

72 current_record="server %s %s weight %s maxconn %s" %(data['record']['server'],\

73 data['record']['server'],\

74 data['record']['weight'],\

75 data['record']['maxconn'])

76 backend_data="backend %s" %backend

77

78 if not record_list:

79 record_list.append(backend_data)

80 record_list.append(current_record)

81 file_handle('haproxy.conf',backend_data,record_list,type='append')

82 else:

83 record_list.insert(0,backend_data)

84 if current_record not in record_list:

85 record_list.append(current_record)

86 file_handle('haproxy.conf',backend_data,record_list,type='change')

87

88 def remove(data):

89 backend=data['backend']

90 record_list=fetch(backend)

91 current_record="server %s %s weight %s maxconn %s" %(data['record']['server'],\

92 data['record']['server'],\

93 data['record']['weight'],\

94 data['record']['maxconn'])

95 backend_data = "backend %s" % backend

96 if not record_list or current_record not in record_list:

97 print('\033[33;1m无此条记录\033[0m')

98 return

99 else:

100 if len(record_list) == 1: #判断record_list是否还有一条记录

101 record_list.remove(current_record) #如果只剩一条记录,就把最后一条记录和backend都删除掉

102 else:

103 record_list.insert(0,backend_data)

104 record_list.remove(current_record)

105 file_handle('haproxy.conf',backend_data,record_list,type='change')

106

107 #{'backend':'www.oldboy20.org','record':{'server':'10.10.0.10','weight':9999,'maxconn':33333333333}}

108

109 def change(data):

110 backend=data[0]['backend']

111 record_list=fetch(backend)

112

113 old_record="server %s %s weight %s maxconn %s" %(data[0]['record']['server'],\

114 data[0]['record']['server'],\

115 data[0]['record']['weight'],\

116 data[0]['record']['maxconn'])

117

118 new_record = "server %s %s weight %s maxconn %s" % (data[1]['record']['server'], \

119 data[1]['record']['server'], \

120 data[1]['record']['weight'], \

121 data[1]['record']['maxconn'])

122 backend_data="backend %s" %backend

123

124 if not record_list or old_record not in record_list:

125 print('\033[33;1m无此内容\033[0m')

126 return

127 else:

128 record_list.insert(0,backend_data)

129 index=record_list.index(old_record)

130 record_list[index]=new_record

131 file_handle('haproxy.conf',backend_data,record_list,type='change')

132

133

134 if __name__ == '__main__':

135 msg='''

136 1:查询

137 2:添加

138 3:删除

139 4:修改

140 5:退出

141 '''

142 menu_dic={

143 '1':fetch,

144 '2':add,

145 '3':remove,

146 '4':change,

147 '5':exit,

148 }

149 while True:

150 print(msg)

151 choice=input("操作>>: ").strip()

152 if len(choice) == 0 or choice not in menu_dic:continue

153 if choice == '5':break

154

155 data=input("数据>>: ").strip()

156

157 #menu_dic[choice](data)==fetch(data)

158 if choice != '1':

159 data=eval(data)

160 menu_dic[choice](data) #add(data)

161

162

163 # [{'backend':'www.oldboy20.org','record':{'server':'2.2.2.3','weight':20,'maxconn':3000}},{'backend':'www.oldboy10.org','record':{'server':'10.10.0.10','weight':9999,'maxconn':33333333333}}]

二、分解实现上面过程:

1 #!/usr/bin/env python

2 # -*- coding:utf-8 -*-

3 #Author: nulige

4

5 import os

6

7 #def file_handle(filename,backend_data,record_list=None,type='fetch'):

8

9 #实现查询功能

10

11 def fetch(data):

12 backend_data="backend %s" %data #backend www.oldboy1.org 查找匹配到这行,找到对应的server记录,用字符串拼接的方法

13 record_list=[] #定义一个空列表

14 with open('haproxy.conf','r') as f: #打开haproxy.conf配置文件

15 tag=False #报警器不响

16 for line in f: #去查找文件

17 if line.strip() == backend_data: #把文件的每一行\n去掉,才能进行比较,就找到这行backend www.oldboy1.org

18 tag=True #找到人就响警报

19 continue #就跳出这个循环

20 if tag and line.startswith('backend'): #找到下一行的backend,backend www.oldboy2.org 我们就跳出循环

21 break

22 if tag and line: #相当于警报响了

23 record_list.append(line.strip()) #找到了server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333,就要把他加到列表中

24 for line in record_list:

25 print(line) #给用户打印找到的信息

26 return record_list #给用户返回处理的信息

27

28 def add(data):

29

30 backend=data['backend']

31 record_list=fetch(backend)

32 current_record = "server %s %s weight %s maxconn %s" % (data['record']['server'], \

33 data['record']['server'], \

34 data['record']['weight'], \

35 data['record']['maxconn'])

36 backend_data = "backend %s" % backend

37

38 if not record_list:

39 record_list.append(backend_data)

40 record_list.append(current_record)

41 with open('haproxy.conf','r') as read_file,\

42 open('haproxy_new.conf','w') as write_file:

43 for r_line in read_file:

44 write_file.write(r_line)

45

46 for new_line in record_list:

47 if new_line.startswith('backend'):

48 write_file.write(new_line+'\n')

49 else:

50 write_file.write("%s%s\n" %(' '*8,new_line))

51

52 else:

53 #处理recard_list

54 record_list.insert(0,backend_data)

55 record_list.append(current_record)

56 with open('haproxy.conf','r') as read_file, \

57 open('haproxy_new.conf','w') as write_file:

58 tag = False

59 has_write = False

60 for r_line in read_file:

61 if r_line.strip() == backend_data:

62 tag = True

63 continue

64 if tag and r_line.startswith('backend'):

65 tag = False

66 if not tag:

67 write_file.write(r_line)

68 else:

69 if not has_write:

70 for new_line in record_list:

71 if new_line.startswith('backend'):

72 write_file.write(new_line + '\n')

73 else:

74 write_file.write("%s%s\n" % (' ' * 8, new_line))

75 has_write = True

76 os.rename('haproxy.conf','haproxy_bak.conf')

77 os.rename('haproxy_new.conf','haproxy.conf')

78 os.remove('haproxy_bak.conf')

79

80 #{'backend':'lhf.oldboy.org','record':{'server':'1.1.1.1','weight':2,'maxconn':200}}

81

82 def remove(data):

83

84 backend=data['backend']

85 record_list=fetch(backend)

86 current_record = "server %s %s weight %s maxconn %s" % (data['record']['server'], \

87 data['record']['server'], \

88 data['record']['weight'], \

89 data['record']['maxconn'])

90 backend_data = "backend %s" % backend

91 if not record_list and current_record not in record_list:

92 print('\033[33:[1m无此条记录\033[0]')

93 return

94 else:

95 #处理recard_list

96 record_list.insert(0,backend_data)

97 record_list.remove(current_record)

98 with open('haproxy.conf','r') as read_file, \

99 open('haproxy_new.conf','w') as write_file:

100 tag = False

101 has_write = False

102 for r_line in read_file:

103 if r_line.strip() == backend_data:

104 tag = True

105 continue

106 if tag and r_line.startswith('backend'):

107 tag = False

108 if not tag:

109 write_file.write(r_line)

110 else:

111 if not has_write:

112 for new_line in record_list:

113 if new_line.startswith('backend'):

114 write_file.write(new_line + '\n')

115 else:

116 write_file.write("%s%s\n" % (' ' * 8, new_line))

117 has_write = True

118 os.rename('haproxy.conf','haproxy_bak.conf')

119 os.rename('haproxy_new.conf','haproxy.conf')

120 os.remove('haproxy_bak.conf')

121

122 #{'backend':'www.oldboy1.org11111','record':{'server':'11.100.200.1','weight':22,'maxconn':200}}

123

124

125 def change(data):

126 backend=data[0]['backend'] #找到要修改的第一条记录

127 record_list=fetch(backend)

128 old_record = "server %s %s weight %s maxconn %s" % (data[0]['record']['server'], \

129 data[0]['record']['server'], \

130 data[0]['record']['weight'], \

131 data[0]['record']['maxconn'])

132

133 new_record = "server %s %s weight %s maxconn %s" % (data[1]['record']['server'], \

134 data[1]['record']['server'], \

135 data[1]['record']['weight'], \

136 data[1]['record']['maxconn'])

137 backend_data = "backend %s" % backend

138 if not record_list and old_record not in record_list: #判断这个文件在原文件中不存在

139 print('\033[33:[1m无此内容\033[0]')

140 return

141 else:

142 record_list.insert(0,backend_data) #判断文件存在原文件中

143 index=record_list.index(old_record) #求出这个索引

144 record_list[index]=new_record #用新的记录,替换掉旧记录

145 with open('haproxy.conf','r') as read_file, \

146 open('haproxy_new.conf','w') as write_file:

147 tag = False

148 has_write = False

149 for r_line in read_file:

150 if r_line.strip() == backend_data:

151 tag = True

152 continue

153 if tag and r_line.startswith('backend'):

154 tag = False

155 if not tag:

156 write_file.write(r_line)

157 else:

158 if not has_write:

159 for new_line in record_list:

160 if new_line.startswith('backend'):

161 write_file.write(new_line + '\n')

162 else:

163 write_file.write("%s%s\n" % (' ' * 8, new_line))

164 has_write = True

165 os.rename('haproxy.conf','haproxy_bak.conf')

166 os.rename('haproxy_new.conf','haproxy.conf')

167 os.remove('haproxy_bak.conf')

168

169 #backend www.oldboy2.org

170 #server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000

171 #[{'backend':'www.oldboy2.org','record':{'server':'3.3.3.3','weight':20,'maxconn':3000}},{'backend':'www.oldboy2.org','record':{'server':'10.10.10.10','weight':30,'maxconn':3000000}}]

172

173 def exit(data):

174 pass

175

176

177 if __name__ == '__main__': #__name__是python的内置变量,他会把__name__赋值给__main__

178 msg='''

179 1:查询

180 2:添加

181 3:删除

182 4: 修改

183 5:退出

184 '''

185 # 定义一个字典,key对应fetch函数,其它也相同

186 menu_dic={

187 '1':fetch,

188 '2':add,

189 '3':remove,

190 '4':change,

191 '5':exit,

192 }

193 while True: #死循环,不断跟用户交互

194 print(msg) #打印出msg信息,让用户选择菜单

195 choice=input("操作>>: ").strip() #去掉空格

196 if len(choice) == 0 or choice not in menu_dic:continue #len(choice)==0,判断他是不是空,choice不在字典里,就跳出这个操作

197 if choice == '5':break

198

199 data=input("数据>>: ").strip() #用户输入的是字符串

200

201 #menu_dic[choice](data)==fetch(data)

202 if choice != '1': #如果输入的不是查询的话,就需要把字符串转换成字典

203 data=eval(data) #因为添加和删除都要求是字典的形式,所以我们要把用户输入的字符串转成字典的形式

204 menu_dic[choice](data) #add(data)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值