Python 연습 - 온라인으로 구성 파일 수정
4719 단어 python 연습
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
# author:stealth
import json,os
def fetch(backend):
backend_title = 'backend %s' %backend
record_list = []
with open('ha.conf','r',encoding='utf-8') as obj:
flag = False
for line in obj:
line = line.strip()
if line == backend_title:
flag = True
continue
if flag and line.startswith('backend'):
flag = False
break
if flag and line:
record_list.append(line)
return record_list
def add(dict_info):
backend = dict_info.get('backend')
record_list = fetch(backend)
backend_title = 'backend %s' %backend
current_record = 'server %s %s weight %s maxconn %s' %(dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
if not record_list:
record_list.append(backend_title)
record_list.append(current_record)
with open('ha.conf','r') as read_f,open('ha.conf.new','w') as write_f:
for line in read_f:
write_f.write(line)
for i in record_list:
if i.startswith('backend'):
write_f.write(i+'
')
else:
write_f.write('%s%s
' %(8*' ',i))
else:
record_list.insert(0,backend_title)
if current_record not in record_list:
record_list.append(current_record)
with open('ha.conf', 'r') as read_f, open('ha.conf.new', 'w') as write_f:
flag = False
has_write = False
for line in read_f:
line_strip = line.strip()
if line_strip == backend_title:
flag = True
continue
if flag and line_strip.startswith('backend'):
flag = False
if not flag:
write_f.write(line)
else:
if not has_write:
for i in record_list:
if i.startswith('backend'):
write_f.write(i+'
')
else:
write_f.write('%s%s
' %(8*' ',i))
has_write = True
def remove(dict_info):
backend = dict_info.get('backend')
record_list = fetch(backend)
backend_title = 'backend %s' %backend
current_record = 'server %s %s weight %s maxconn %s' % (dict_info['record']['server'], dict_info['record']['server'], dict_info['record']['weight'], dict_info['record']['maxconn'])
if not record_list:
return
else:
if current_record not in record_list:
return
else:
del record_list[record_list.index(current_record)]
if len(record_list) > 0:
record_list.insert(0,backend_title)
with open('ha.conf') as read_f,open('ha.conf.new','w') as write_f:
flag = False
has_write = False
for line in read_f:
line_strip = line.strip()
if line_strip == backend_title:
flag = True
continue
if flag and line_strip.startswith('backend'):
flag = False
if not flag:
write_f.write(line)
else:
if not has_write:
for i in record_list:
if i.startswith('backend'):
write_f.write(i+'
')
else:
write_f.write('%s%s
' %(8*' ',i))
has_write = True
if __name__ == '__main__':
print('1、 ;2、 ;3、 ')
num = input(' :')
if num == '1':
data = input(' :')
print(fetch(data))
elif num == '2':
data = input(' :')
dict_data = json.loads(data)
add(dict_data)
elif num == '3':
data = input(' :')
dict_data = json.loads(data)
remove(dict_data)
else:
print(' !')
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
python+selenium텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.