需求如下:
1、動態的查詢新增刪除haproxy節點資訊
2、程式功能:add(新增)、del(刪除)、query(查詢)
3、新增時例項字串為: }
**:
1#author:lee sir
2import
sys3
45 haproxyconf = r"
e:\python練習指令碼\day3\haproxy.txt
"6 haproxyconf_temp = r"
e:\python練習指令碼\day3\haproxy_temp.txt"7
8def
openconf(file,mode):
9 fd = open(file,'
%s' %mode)
10return
fd11
12def
closeconf(fd):
13fd.close()
1415
defuser_choice():
16 choice = ['
query
','add
','delete
','update']
17for index,key in
enumerate(choice):
18print
(index,key)
19while
true:
20 userchoice = input('
please number for your choice: ')
21if userchoice.isdigit() and int(userchoice) >= 0 and int(userchoice) < 3:
22return
userchoice
23elif userchoice == 'q'
or userchoice == 'q'
:24 sys.exit('
bye bye')
25else:26
print('
input error,please try again')
2728
deflistnode():
29 conf = openconf(haproxyconf,'r'
)30 key = input('
please input your query node name: ')
31 count =0
32for line in
conf:
33if key in line and line.strip().startswith('
backend'):
34 result =true
35 nodenumber = 1
36while
result:
37 nodeinfo =conf.readline()
38if nodeinfo.strip().startswith('
server'):
39print('
節點 %s:%s
' %(nodenumber,nodeinfo.strip()))
40 count += 1
41 nodenumber += 1
42else
:43 result =false
44if count ==0:
45print('
\033[31;1mno node for %s\033[0m
' %key)
46closeconf(conf)
4748
defdelenode():
49 conf = openconf(haproxyconf,'r'
)50 key = input('
please input delete node: ')
51 conf_list =
52for line in
conf:
53if line.strip().startswith('
backend'):
54if key in
line:
5556
while
true:
57 nodeinfo =conf.readline()
58if nodeinfo.strip().startswith('
server'):
5960
else:61
break
62else:63
if len(conf_list) >0:
64 old_conf = openconf(haproxyconf,'r'
)65 write_conf = openconf(haproxyconf_temp,'w'
)66for info in
old_conf:
67if info in
conf_list:
68continue
69else:70
write_conf.write(info)
71else:72
closeconf(old_conf)
73closeconf(write_conf)
74print('
delete node:%s successful!
' %key )
75else
:76 sys.exit('
no node info for %s
' %key)
7778
defaddnode():
79 conf = openconf(haproxyconf_temp,'r'
)80#nodeinfo = '''}'''
81 nodeinfo = input('
please input your add node info:')
82 nodeinfo =eval(nodeinfo)
83if
isinstance(nodeinfo,dict):
84 backend = nodeinfo['
backend']
85for line in
conf:
86if line.strip().startswith('
backend'):
87if backend in
line:
88 sys.exit('
the node are exist!')
89else
:90 add_wirte = openconf(haproxyconf_temp,'a'
)91 backend = nodeinfo['
backend']
92 record = nodeinfo['
record']
93 server = record['
server']
94 weight = record['
weight']
95 maxconn = record['
maxconn']
96 node_info = '''
\nbackend %s\n\t\tserver %s %s weight %s maxconn %s
97''' %(backend,server,server,weight,maxconn)
98add_wirte.write(node_info)
99print('
the node:%s add successful!
' %backend )
100else
:101 sys.exit('
input error,bye bye!')
102103
104def
main():
105 userchoice =user_choice()
106if userchoice == '0'
:107
listnode()
108elif userchoice == '1'
:109
addnode()
110elif userchoice == '2'
:111
delenode()
112113
114if
__name__ == '
__main__':
115 main()
posted @
2017-03-17 22:27
dahlhin 閱讀(
...)
編輯收藏
小程式 動態修改二維陣列 示例
1.首先獲取使用者所有未刪除的位址 根據字段status進行判斷 規定 0 普通位址,1 預設位址,2 已刪除位址 2.當進行刪除操作成功後,遍歷位址陣列列表,找到對應操作成功的位址id,然後動態js更改其status狀態值 其實此處的狀態值只是乙個標識,也可以使用 hide 或 show 來進行區...
運動目標檢測小程式之修改版
而且效果也比上次好多鳥 敬請期待生成運動軌跡!include include include include 必須引此標頭檔案1 define threshold diff1 20 設定簡單幀差法閾值 define threshold diff2 20 設定簡單幀差法閾值 using namespa...
Python小程式解析之 函式 powersum
def powersum power,args return the sum of each argument raised to specified power.total 0 for i in args total pow i,power return total powersum 2,3,4 ...