1 tag = True 2 while tag: 3 # print('Please input: 1\n2\n3\n4\n') 4 if tag: 5 print ( ' level ' ) 6 choice = input( ' Please input: ' ) 7 if choice== ' quit ' : 8 break 9 elif choice== ' quit_all ' : 10 tag = False 11 while tag: 12 print( ' level2 ' ) 13 choice = input( ' Please input: ' ) 14 if choice == ' quit ' : 15 break 16 elif choice == ' quit_all ' : 17 tag = False 18 while tag: 19 print ( ' level3 ' ) 20 choice = input( ' Please enter: ' ) 21 ifchoice == ' quit ' : 22 break 23 elif choice == ' quit_all ' : 24 tag = False 25 26 def fetch(data): 27 print ( ' This is the query ' ) 28 # print('%s this is the data to be queried' %data) 29 backend_data = ' backend %s ' % data 30 with open( ' haproxy.conf ' , ' r ') as read_f: 31 tag = False 32 for read_line in read_f: 33 if read_line.strip() == backend_data : 34 tag = True #If the input content is the same as the found content, change the tag state and recycle 35 continue 36 if tag and read_line.startswith( ' backend ' ): 37 break #If the next backend is encountered, terminate the loop 38 if tag: 39 print (read_line,end= ' ' ) # Print one by one until the break occurs 40 41 def add(): 42 print ( ' This is an addition ' ) 43 44 def change(): 45 print ( ' This is a change ' ) 46 47 def delete(): 48 print ( ' this is a delete ' ) 49 50 if __name__ == ' __main__ ' : 51 msg= ''' 52 1: query 53 2: add 54 3: modify 55 4: delete 56 5: exit 57 ''' 58 msg_dic = { 59 ' 1 ' :fetch, 60 ' 2 ' :add, 61 ' 3 ' :change, 62 ' 4 ' :delete 63 } 64 while True: 65 print (msg) 66 choice = input( ' Please input: ' ).strip() 67 if not choice: continue #If the user does not input content, re-loop 68 if choice == ' 5 ' : break 69 data = input( ' Please enter the content to be queried ' ) 70 msg_dic[choice](data) #If the user input is '1', Then it becomes msg_dic[1]() == fetch() function