代碼需求:
-
- 可查詢haproxy配置文件
- 可增加相關網址及記錄
- 可刪除相關網址及記錄
- 可修改相關網址及記錄
haproxy文件:
global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000
backend www.sina.com
server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
haproxy
代碼展示:
功能實現:
1、輸入相關網址,可查詢相關記錄信息;
2、按照字典樣式輸入網址及記錄信息,可在haproxy文件中增加;
3、輸入相關網址,可刪除相關記錄信息;
4、按照字典樣式輸入網址及記錄信息,可在haproxy文件中更新。
程序目錄:
程序代碼:
第一版代碼:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 # 查詢 6 def search(website): 7 result_list = [] #存放查詢的結果 8 search_flag = False #定義查詢結果標志 9 with open('haproxy','r',encoding='utf-8') as f: 10 for line in f: 11 # if 語句順序進行,當前條件判斷完,進入下一判斷 12 if 'backend {}'.format(website) == line.strip(): #找到backend www.oldboy.org 開始截取 13 search_flag = True 14 if line.strip().startswith('backend') and line.strip() != 'backend {}'.format(website): 15 #找到下一個 backend www.xxx.com部分結束,截取其中部分 16 search_flag = False 17 if search_flag: 18 result_list.append(line.strip()) 19 print(result_list) 20 f.close() 21 if result_list == []: 22 print("對不起,您查詢的網址不存在!") 23 else: 24 return result_list 25 26 # 新增 27 def add(website): 28 arg = eval(website) #將輸入的字符串轉變為字典 29 # print(arg) 30 backend_title = "backend {}".format(arg['backend']) # 要插入backend的字段 31 # print(backend_title) 32 record_title = arg["record"] 33 context_record = "server {0} {0} weight {1} maxconn {2}".\ 34 format(record_title['server'],record_title['weight'],record_title['maxconn']) 35 # print(context_record) 36 37 add_flag = True #設置新增標志位 38 with open('haproxy', 'r+', encoding='utf-8') as f: 39 for line in f: 40 if line.strip() == backend_title: 41 print("您新增的網址已經存在!") 42 add_flag = False 43 if add_flag: 44 f.write('\n{}'.format(backend_title)) 45 f.write('\n\t\t{}'.format(context_record)) 46 f.close() 47 48 # 刪除 49 def delete(website): 50 51 delete_flag = False #設置刪除標志位 52 with open('haproxy', 'r', encoding='utf-8') as f,\ 53 open('haproxy_bak','w') as f1 : 54 for line in f: 55 if 'backend {}'.format(website) == line.strip(): 56 delete_flag = True 57 continue #如果出現修改backend_title,設置修改標志位為True。 58 if line.strip().startswith('backend') and line.strip() != website : 59 delete_flag = False #捕捉到下一個backend_title,設置修改標志位為False。 60 if not delete_flag: 61 f1.write(line) 62 63 # if line.strip() != backend_title and line.strip() != context_record: 64 # f1.write(line) #如果文件內容行不等於要刪除內容,寫入備份文件中 65 66 if delete_flag == False: 67 print("您刪除的網址不存在!") 68 69 with open('haproxy', 'w') as f,\ 70 open('haproxy_bak','r',encoding='utf-8') as f1 : #將備份文件寫入原文件中 71 for line in f1: 72 f.write(line) 73 #修改 74 def update(website): 75 arg = eval(website) 76 # print(arg) 77 backend_title = "backend {}".format(arg['backend']) # 要插入backend整個字段 78 # print(backend_title) 79 record_title = arg["record"] 80 context_record = "server {0} {0} weight {1} maxconn {2}". \ 81 format(record_title['server'], record_title['weight'], record_title['maxconn']) 82 # print(context_record) 83 84 update_flag = False #設置修改標志位 85 update_re = False #設置重復修改位 86 87 with open('haproxy', 'r', encoding='utf-8') as f, \ 88 open('haproxy_bak', 'w') as f1: 89 for line in f: 90 if line.strip() == backend_title: 91 update_flag = True 92 continue #如果出現修改backend_title,設置修改標志位為True。 93 if line.strip().startswith('backend') and line.strip() != backend_title : 94 update_flag = False #捕捉到下一個backend_title,設置修改標志位為False。 95 if not update_flag: 96 f1.write(line) 97 if update_flag and not update_re: #修改標志位為True,並且沒有被修改過 98 f1.write('\n{}'.format(backend_title)) 99 f1.write('\n\t\t{}\n'.format(context_record)) 100 update_re = True 101 102 with open('haproxy', 'w') as f, \ 103 open('haproxy_bak', 'r', encoding='utf-8') as f1: 104 for line in f1: 105 f.write(line) 106 107 108 #主函數 109 while True: 110 print("歡迎進入haproxy配置程序".center(50, "#"), 111 "\n1 查詢\n" 112 "2 新增\n" 113 "3 刪除\n" 114 "4 修改\n" 115 "q 退出程序\n") 116 op_haproxy = input("選擇要進入模式的ID:") 117 if op_haproxy == '1': 118 website = input("請輸入要查詢的網址:" 119 "例如:www.oldboy.org\n") 120 search(website) 121 elif op_haproxy == '2': 122 website = input("請輸入要新增的網址配置:" 123 "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8'," 124 "'weight': 20,'maxconn': 3000}}\n") 125 add(website) 126 elif op_haproxy == '3': 127 website = input("請輸入要刪除的網址配置:" 128 "例如:www.baidu.com\n") 129 delete(website) 130 elif op_haproxy =='4': 131 website = input("請輸入要修改的網址配置:" 132 "例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8'," 133 "'weight': 20,'maxconn': 3000}}\n") 134 update(website) 135 elif op_haproxy == 'q': 136 break 137 else: 138 print("請檢查您的輸入!")
執行效果:
1 E:\Python\PythonExercising\haproxy\venv\Scripts\python.exe E:/Python/PythonExercising/haproxy/haproxy.py 2 #################歡迎進入haproxy配置程序################## 3 1 查詢 4 2 新增 5 3 刪除 6 4 修改 7 q 退出程序 8 9 選擇要進入模式的ID:1 10 請輸入要查詢的網址:例如:www.oldboy.org 11 www.oldboy.org 12 ['backend www.oldboy.org', 'server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000'] 13 #################歡迎進入haproxy配置程序################## 14 1 查詢 15 2 新增 16 3 刪除 17 4 修改 18 q 退出程序 19 20 選擇要進入模式的ID:2 21 請輸入要新增的網址配置:例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 22 {'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 23 #################歡迎進入haproxy配置程序################## 24 1 查詢 25 2 新增 26 3 刪除 27 4 修改 28 q 退出程序 29 30 選擇要進入模式的ID:3 31 請輸入要刪除的網址配置:例如:www.baidu.com 32 www.baidu.com 33 #################歡迎進入haproxy配置程序################## 34 1 查詢 35 2 新增 36 3 刪除 37 4 修改 38 q 退出程序 39 40 選擇要進入模式的ID:4 41 請輸入要修改的網址配置:例如:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 42 {'backend': 'www.oldboy.org','record': {'server': '100.1.7.5','weight': 20,'maxconn': 3000}} 43 #################歡迎進入haproxy配置程序################## 44 1 查詢 45 2 新增 46 3 刪除 47 4 修改 48 q 退出程序 49 50 選擇要進入模式的ID:q 51 52 Process finished with exit code 0
新增后配置文件:

global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000 backend www.sina.com server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000 backend www.baidu.com server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000
刪除后配置文件:

global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.8 100.1.7.8 weight 20 maxconn 3000 backend www.sina.com server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
修改后配置文件:

global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 info defaults log global mode http timeout connect 5000ms timeout client 50000ms timeout server 50000ms option dontlognull listen stats :8888 stats enable stats uri /admin stats auth admin:1234 frontend oldboy.org bind 0.0.0.0:80 option httplog option httpclose option forwardfor log global acl www hdr_reg(host) -i www.oldboy.org use_backend www.oldboy.org if www backend www.oldboy.org server 100.1.7.5 100.1.7.5 weight 20 maxconn 3000 backend www.sina.com server 100.1.7.7 100.1.7.7 weight 20 maxconn 3000
第二版代碼:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 def if_continue(): # 定義函數if_continue() 提示用戶是否繼續操作 6 if_cont = input("\n\33[34;1mDo you want to continue to operate on files【y】/【n】:\33[0m\n") 7 if if_cont == "y": 8 pass 9 else: 10 exit() 11 12 13 def info_message(options): # 定義函數info_message() 提示用戶操作信息 14 print("\33[31;1mInfo of %s\33[0m".center(50, "-") % options) 15 16 17 with open("haproxy", "a+", encoding="utf-8") as file_haproxy: # a+模式打開haproxy文件 18 while True: # 設置while循環 19 dict_file = {} 20 file_haproxy.seek(0) # 移動光標到文件首部 21 for line in file_haproxy: 22 if "backend" in line and "use_backend" not in line: # 提前文件中backend信息並生成字典dict_file 23 dict_file[line.split()[1]] = file_haproxy.readline().strip() 24 25 print("File_Operations_Backend".center(50, "*"), "\n1\tQuery\n2\tAdd\n3\tDel\nq\tQuit") 26 user_choice = input("\33[34;1mSelect the ID to operate:\33[0m") # 讓用戶選擇操作文件的模式 27 28 if user_choice == "1": 29 info_query = input("\33[34;1mInput information to query:\33[0m") 30 if info_query in dict_file.keys(): # 判斷輸入的查詢的信息是否存在 31 info_message("Query") 32 print(dict_file[info_query]) # 如果查詢的backend存在 打印查詢的信息 33 else: # 否則提示沒有查詢到相關信息 34 print("\33[31;1mError:No query to the corresponding information!\33[0m") 35 if_continue() 36 37 elif user_choice == "2": 38 info_add = input("\33[34;1mInput information to add:\33[0m") 39 try: # 判斷輸入的類型是否可以轉換成字典格式 40 dict_add = eval(info_add) # 字符串轉換成字典 41 if dict_add["backend"] not in dict_file.keys(): # 判斷新增的信息沒有存在於文件中 42 dict_add_record = dict_add["record"] # 把要添加的信息定義到變量file_add 中 43 file_add = "backend %s\n\t\tserver %s weight %s maxconn %s\n" % (dict_add["backend"], 44 dict_add_record["server"], 45 dict_add_record["weight"], 46 dict_add_record["maxconn"],) 47 file_haproxy.write(file_add) # 把新增的信息寫到文件中 48 info_message("Add") # 打印增加成功 49 print("\33[32;1mSuccessfully adding information backend %s to a file\33[0m" % (dict_add["backend"])) 50 else: # 如果已經存在 打印信息已經存在 51 print("\33[31;1mError:Add the information already exists!\33[0m") 52 if_continue() 53 except Exception: # 如果輸入的字符不能轉換為字典格式 提示錯誤 54 print("\33[31;1mError:Please enter the dict format!\33[0m") 55 if_continue() 56 57 elif user_choice == "3": 58 info_del = input("\33[34;1mInput information to del:\33[0m") 59 try: # 判斷輸入的類型是否可以轉換成字典格式 60 dict_del = eval(info_del) # 字符串轉換成字典 61 if dict_del["backend"] in dict_file.keys(): # 判斷要刪除的信息有沒有存在於文件中 62 file_haproxy.seek(0) 63 list_del = file_haproxy.readlines() # 把文件信息寫入列表list_del 64 index = list_del.index("backend %s\n" % (dict_del["backend"])) # 獲取要刪除信息的下標 65 del list_del[index] # 在列表中刪除輸入信息 66 del list_del[index] 67 file_haproxy.seek(0) 68 file_haproxy.truncate(0) # 文件清空 69 for line in list_del: # 把list_del內容寫入到文件中 70 file_haproxy.write(line) 71 info_message("Del") # 提示刪除成功 72 print("\33[32;1mSuccessfully delect information backend %s to a file\33[0m" % (dict_del["backend"])) 73 74 else: # 如果要刪除的信息不再文件中,打印信息不存在 75 print("\33[31;1mError:Delect the information is not exists!\33[0m") 76 if_continue() 77 except Exception: # 如果輸入的字符不能轉換為字典格式 提示錯誤 78 print("\33[31;1mError:Please enter the dict format!\33[0m") 79 if_continue() 80 81 elif user_choice == "q": 82 print("\33[31;1mExit\33[0m".center(30, "-")) 83 exit() 84 85 else: 86 print("\33[31;1mError:Select the ID does not exist!\33[0m")
執行效果:
1 E:\Python\PythonExercising\haproxy\venv\Scripts\python.exe E:/Python/PythonExercising/haproxy/haproxy1.py 2 *************File_Operations_Backend************** 3 1 Query 4 2 Add 5 3 Del 6 q Quit 7 Select the ID to operate:1 8 Input information to query:www.oldboy.org 9 --------------Info of Query--------------- 10 server 100.1.7.5 100.1.7.5 weight 20 maxconn 3000 11 12 Do you want to continue to operate on files【y】/【n】: 13 y 14 *************File_Operations_Backend************** 15 1 Query 16 2 Add 17 3 Del 18 q Quit 19 Select the ID to operate:2 20 Input information to add:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 21 --------------Info of Add--------------- 22 Successfully adding information backend www.baidu.com to a file 23 24 Do you want to continue to operate on files【y】/【n】: 25 y 26 *************File_Operations_Backend************** 27 1 Query 28 2 Add 29 3 Del 30 q Quit 31 Select the ID to operate:3 32 Input information to del:{'backend': 'www.baidu.com','record': {'server': '100.1.7.8','weight': 20,'maxconn': 3000}} 33 --------------Info of Del--------------- 34 Successfully delect information backend www.baidu.com to a file 35 36 Do you want to continue to operate on files【y】/【n】: 37 y 38 *************File_Operations_Backend************** 39 1 Query 40 2 Add 41 3 Del 42 q Quit 43 Select the ID to operate:q 44 -------Exit-------- 45 46 Process finished with exit code 0
總結提高:
1、不斷優化程序結果,更符合用戶習慣;
2、可以先將原文件遍歷,將有關信息存儲為字典,方便后續條件的判斷;
3、r+模式下,如果在.write()進行寫入內容前,沒有print()輸出,則要寫的內容會從文件頭部開始寫入;
如果在.write()進行寫入內容前,有print()輸出,則要寫的內容會從文件尾部開始寫入。
4、eval函數的用法:
官方解釋為:將字符串str當成有效的表達式來求值並返回計算結果。
1、計算字符串中有效的表達式,並返回結果。
1 >>> eval('pow(2,2)') 2 4 3 >>> eval('2 + 2') 4 4
2、將字符串轉成相應的對象(如list、tuple、dict和string之間的轉換)。
1 >>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" 2 >>> b = eval(a) 3 >>> b 4 [[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] 5 >>> a = "{1:'xx',2:'yy'}" 6 >>> c = eval(a) 7 >>> c 8 {1: 'xx', 2: 'yy'} 9 >>> a = "(1,2,3,4)" 10 >>> d = eval(a) 11 >>> d 12 (1, 2, 3, 4)