需求如下:
1、动态的查询添加删除haproxy节点信息
2、程序功能:add(添加)、Del(删除)、Query(查询)
3、添加时实例字符串为: {'backend': 'www.oldboy.com','record': {'server': '100.1.7.9','weight': 20,'maxconn': 30}}
代码:
1 # Author:Lee Sir 2 import sys 3 4 5 haproxyconf = r"E:Python练习脚本day3haproxy.txt" 6 haproxyconf_temp = r"E:Python练习脚本day3haproxy_temp.txt" 7 8 def openconf(file,mode): 9 fd = open(file,'%s' % mode) 10 return fd 11 12 def closeconf(fd): 13 fd.close() 14 15 def user_choice(): 16 choice = ['Query','Add','Delete','Update'] 17 for index,key in enumerate(choice): 18 print(index,key) 19 while True: 20 userchoice = input('Please number for your Choice: ') 21 if userchoice.isdigit() and int(userchoice) >= 0 and int(userchoice) < 3: 22 return userchoice 23 elif userchoice == 'q' or userchoice == 'Q': 24 sys.exit('Bye Bye') 25 else: 26 print('Input Error,Please try again') 27 28 def listnode(): 29 conf = openconf(haproxyconf,'r') 30 key = input('Please Input Your Query Node Name: ') 31 count = 0 32 for line in conf: 33 if key in line and line.strip().startswith('backend'): 34 result = True 35 nodenumber = 1 36 while result: 37 nodeinfo = conf.readline() 38 if nodeinfo.strip().startswith('server'): 39 print('节点 %s:%s ' % (nodenumber,nodeinfo.strip())) 40 count += 1 41 nodenumber += 1 42 else: 43 result = False 44 if count == 0: 45 print('