日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

Python蜕变-2017-4-23

發(fā)布時(shí)間:2025/3/15 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python蜕变-2017-4-23 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  我的第一篇博客,這是試水練習(xí)。這次上的菜是Haporxy配置文件操作。

<1> 上需求:

具體配置文件如下:

1 global 2 log 127.0.0.1 local2 3 daemon 4 maxconn 256 5 log 127.0.0.1 local2 info 6 defaults 7 log global 8 mode http 9 timeout connect 5000ms 10 timeout client 50000ms 11 timeout server 50000ms 12 option dontlognull 13 14 listen stats :8888 15 stats enable 16 stats uri /admin 17 stats auth admin:1234 18 19 frontend oldboy.org 20 bind 0.0.0.0:80 21 option httplog 22 option httpclose 23 option forwardfor 24 log global 25 acl www hdr_reg(host) -i www.oldboy.org 26 use_backend www.oldboy.org if www 27 28 backend www.oldboy.org 29 server 100.1.7.9 weight 20 maxconn 3000 View Code

?

<2>分析需求:

  1.將配置文件中的主要信息也就是“backend”后的數(shù)據(jù)展示出來。

  2.能夠?qū)?shù)據(jù)進(jìn)行增刪改查的操作。

  3.每次操作后進(jìn)行備份。

<3>功能分析:

  1.讀取配置文件并截取有用數(shù)據(jù)打印。

  2.具備增刪改查功能,在此有用戶輸入,所以有必要進(jìn)行判斷,防止錯(cuò)誤輸入導(dǎo)致程序崩潰。

  3.將操作后的數(shù)據(jù)與原始的無關(guān)數(shù)據(jù)進(jìn)行拼接寫入配置文件

<4>完整代碼:

1 #--------------------------Haporxy配置文件操作-------------------------------------- 2 #py3.4 by:羽凡 2017-4-23 3 #----------------------------------------------讀取文件處理數(shù)據(jù)(返回字典Info{})------- 4 def Deal_file(): 5 s = "" 6 Info = {} 7 with open("配置文件.txt","r") as f: 8 for i in f: 9 if i.startswith("backend"): 10 backend = i.strip().split(" ")[1] 11 Info[backend] = [] 12 elif i.strip().startswith("server"): 13 server = i.strip().split(" ")[1] 14 weight = i.strip().split(" ")[3] 15 maxconn = i.strip().split(" ")[5] 16 Server = {"server":server,"weight":weight,"maxconn":maxconn} 17 Info[backend].append(Server) 18 else: 19 continue 20 return Info 21 #----------------------------------------------信息保存----------------------------- 22 def Save_Info(Info): 23 s = "" 24 with open("配置文件.txt","r") as f: 25 for i in f: 26 if not i.startswith("backend"): 27 s = s + i 28 else: 29 break 30 s1 = "" 31 for backend in Info: 32 s2 = "backend " + backend 33 s1 = s1 + s2 + "\n" 34 for di in Info[backend]: 35 server = di["server"] 36 weight = di["weight"] 37 maxconn = di["maxconn"] 38 s3 = " server {} weight {} maxconn {}".format(server,weight,maxconn) 39 s1 = s1 + s3 + "\n" 40 S = s + s1 41 with open("配置文件.txt","w+") as F: 42 F.write(S) 43 return(S) 44 #----------------------------------------------展示信息,返回指令--------------------- 45 def Show(Info): 46 show_list = [] 47 print("--------------------""\033[1;31;0m""HaProxy配置文件管理""\033[0m""-------------------------------") 48 for i,n in enumerate(Info): 49 show_list.append(n) 50 print("\033[1;32;0m",i+1,n,"\033[0m") 51 print("------------------------------------------------------------------------") 52 order = input("請輸入指令(A)新建、(D)刪除、(M)修改、(C)查詢、(Q)退出:") 53 while order not in ("A","D","M","C","Q"): 54 order = input("\033[1;31;0m""輸入錯(cuò)誤,請重新輸入:""\033[0m") 55 show_list = list(show_list) 56 return order,show_list 57 #----------------------------------------------Backend編號輸入判斷------------------- 58 def Jude(choice,show_list): 59 try: 60 if eval(choice) not in range(len(show_list)+1): 61 return True 62 else:return False 63 except: 64 return True 65 #----------------------------------------------Server編號判斷----------------------- 66 def JudeS(choiceS,a): 67 try: 68 if int(choiceS) in range(1,a+1) or int(choiceS) == 1: 69 return False 70 else: 71 return True 72 except: 73 return True 74 #----------------------------------------------新增信息判斷-------------------------- 75 def JudeA(backend,weight,maxconn): 76 try: 77 if backend.startswith("www") and int(weight) in range(50) and int(maxconn) in range(5000): 78 return False 79 except: 80 return True 81 #----------------------------------------------查詢--------------------------------- 82 def Check(show_list): 83 choice = input("請選擇backend:") 84 while Jude(choice,show_list): 85 choice = input("\033[1;31;0m""輸入錯(cuò)誤,請重新輸入:""\033[0m") 86 choice = int(choice) 87 print("------------------------------",show_list[choice-1],"------------------------------") 88 for a,b in enumerate(Info[show_list[choice-1]]): 89 print("{} server:{} weight:{} maxconn:{}".format(a+1,b["server"],b["weight"],b["maxconn"])) 90 print("----------------------------------------------------------------------------") 91 select = input("請輸入指令(Q)退出、(B)返回:") 92 while select not in ("Q","B"): 93 select = input("\033[1;31;0m""請輸入正確指令:""\033[0m") 94 if select == "Q": 95 pass 96 elif select == "B": 97 main() 98 #----------------------------------------------刪除--------------------------------- 99 def Delete(show_list): 100 choice = input("刪除Backend(B) or Server(S)?:") 101 while(choice not in ("B","S")): 102 choice = input("\033[1;31;0m""請輸入正確的指令:""\033[0m") 103 selection = input("請選擇Backend編號:") 104 while Jude(selection,show_list): 105 selection = input("\033[1;31;0m""請輸入正確的編號:""\033[0m") 106 selection = int(selection) 107 if choice =="B": 108 del Info[show_list[selection-1]] 109 Save_Info(Info) 110 print("\033[1;31;0m""刪除成功!""\033[0m") 111 main() 112 elif choice == "S": 113 print("------------------------------",show_list[selection-1],"------------------------------") 114 for a,b in enumerate(Info[show_list[selection-1]]): 115 print("{} server:{} weight:{} maxconn:{}".format(a+1,b["server"],b["weight"],b["maxconn"])) 116 print("----------------------------------------------------------------------------") 117 choiceS = input("選擇要?jiǎng)h除的Server:") 118 while JudeS(choiceS,a): 119 choiceS = input("\033[1;31;0m""請輸入正確的編號:""\033[0m") 120 choiceS = int(choiceS) 121 Info[show_list[selection-1]].pop(choiceS-1) 122 Save_Info(Info) 123 print("\033[1;31;0m""刪除成功!""\033[0m") 124 main() 125 #----------------------------------------------增加--------------------------------- 126 def Add(): 127 print("————>請輸入詳細(xì)信息<————") 128 backend = input("Backend名稱:") 129 server = input("Server名稱:") 130 weight = input("權(quán)重(weight):") 131 maxconn = input("最大鏈接數(shù)(maxconn):") 132 while JudeA(backend,weight,maxconn): 133 print("\033[1;31;0m""輸入信息錯(cuò)誤,請重新輸入!!!""\033[0m") 134 Add() 135 if backend not in Info: 136 Info[backend] = [{"server":server,"weight":weight,"maxconn":maxconn}] 137 print(">>>>>新建成功") 138 else: 139 flag = 1 140 for ser in Info[backend]: 141 if server == ser["server"]: 142 flag = 0 143 ser["weight"] = weight 144 ser["maxconn"] = maxconn 145 print(">>>>>backend及server信息已存在,""\033[1;31;0m""server信息已修改""\033[0m") 146 if flag: 147 Info[backend].append({"server":server,"weight":weight,"maxconn":maxconn}) 148 print(">>>>>backend已存在,""\033[1;31;0m""server添加成功""\033[0m") 149 Save_Info(Info) 150 main() 151 #----------------------------------------------修改--------------------------------- 152 def Modify(show_list): 153 selection = input("修改backend(B) or server(S)?>") 154 while (selection not in ("B","S")): 155 selection = input("\033[1;31;0m""請輸入正確回答:""\033[0m") 156 choice = input("請輸入要修改的backend編號:") 157 while Jude(choice,show_list): 158 choice = input("\033[1;31;0m""請輸入正確編號:""\033[0m") 159 choice = int(choice) 160 if selection == "B": 161 backend = input("修改backend名稱為:") 162 while not backend.startswith("www"): 163 backend = input("\033[1;31;0m""請輸入合法的backend名稱:""\033[0m") 164 temp = Info[show_list[choice-1]] 165 del Info[show_list[choice-1]] 166 Info[backend] = temp 167 Save_Info(Info) 168 print("\033[1;31;0m""修改backend成功""\033[0m") 169 main() 170 else: 171 print("------------------------------",show_list[choice-1],"------------------------------") 172 for a,b in enumerate(Info[show_list[choice-1]]): 173 print("{} server:{} weight:{} maxconn:{}".format(a+1,b["server"],b["weight"],b["maxconn"])) 174 print("----------------------------------------------------------------------------") 175 selection = input("修改哪個(gè)server?>") 176 while JudeS(selection,a): 177 selection = input("\033[1;31;0m""請輸入正確的server編號:""\033[0m") 178 selection = int(selection) 179 sername = input("修改server為:") 180 Info[show_list[choice-1]][a-1]["server"] = sername 181 Save_Info(Info) 182 print("\033[1;31;0m""修改server成功""\033[0m") 183 main() 184 #----------------------------------------------指令處理------------------------------ 185 def Deal_order(order,show_list): 186 if order =="Q": #退出 187 pass 188 elif order =="A" : #新建 189 Add() 190 elif order == "D": #刪除 191 Delete(show_list) 192 elif order == "M": #修改 193 Modify(show_list) 194 elif order == "C": 195 Check(show_list) 196 else: 197 order = input("\033[1;31;0m""輸入錯(cuò)誤,請重新輸入:""\033[0m") 198 Deal_order(order,show_list) 199 #----------------------------------------------主函數(shù)-------------------------------- 200 def main(): 201 order,show_list = Show(Info) 202 Deal_order(order,show_list) 203 if __name__=="__main__": 204 Info = Deal_file() 205 main() View Code

<5>處理流程:

?

<6>主要函數(shù)功能介紹:

?  1.Deal_file():讀取配置文件,并截取操作數(shù)據(jù),通過String.startswith()找到截取位置獲取數(shù)據(jù),返回字典形式。

1 def Deal_file(): 2 s = "" 3 Info = {} 4 with open("配置文件.txt","r") as f: 5 for i in f: 6 if i.startswith("backend"): 7 backend = i.strip().split(" ")[1] 8 Info[backend] = [] 9 elif i.strip().startswith("server"): 10 server = i.strip().split(" ")[1] 11 weight = i.strip().split(" ")[3] 12 maxconn = i.strip().split(" ")[5] 13 Server = {"server":server,"weight":weight,"maxconn":maxconn} 14 Info[backend].append(Server) 15 else: 16 continue 17 return Info View Code

  2.Save_Info():將操作后的數(shù)據(jù)與原始無關(guān)數(shù)據(jù)拼接保存,接收字典作為參數(shù)。

1 def Save_Info(Info): 2 s = "" 3 with open("配置文件.txt","r") as f: 4 for i in f: 5 if not i.startswith("backend"): 6 s = s + i 7 else: 8 break 9 s1 = "" 10 for backend in Info: 11 s2 = "backend " + backend 12 s1 = s1 + s2 + "\n" 13 for di in Info[backend]: 14 server = di["server"] 15 weight = di["weight"] 16 maxconn = di["maxconn"] 17 s3 = " server {} weight {} maxconn {}".format(server,weight,maxconn) 18 s1 = s1 + s3 + "\n" 19 S = s + s1 20 with open("配置文件.txt","w+") as F: 21 F.write(S) 22 return(S) View Code

  3.Show():將要操作的數(shù)據(jù)打印出來提示輸入操作指令,白名單判斷輸入指令,進(jìn)行判斷,返回輸入指令和打印列表。

  效果:

1 def Show(Info): 2 show_list = [] 3 print("--------------------""\033[1;31;0m""HaProxy配置文件管理""\033[0m""-------------------------------") 4 for i,n in enumerate(Info): 5 show_list.append(n) 6 print("\033[1;32;0m",i+1,n,"\033[0m") 7 print("------------------------------------------------------------------------") 8 order = input("請輸入指令(A)新建、(D)刪除、(M)修改、(C)查詢、(Q)退出:") 9 while order not in ("A","D","M","C","Q"): 10 order = input("\033[1;31;0m""輸入錯(cuò)誤,請重新輸入:""\033[0m") 11 show_list = list(show_list) 12 return order,show_list View Code

  4.Jude()、JudeA()、JudeS()分別對輸入的backend編號、server編號和新建的數(shù)據(jù)信息進(jìn)行簡單判斷。

1 #----------------------------------------------Backend編號輸入判斷------------------- 2 def Jude(choice,show_list): 3 try: 4 if eval(choice) not in range(len(show_list)+1): 5 return True 6 else:return False 7 except: 8 return True 9 #----------------------------------------------Server編號判斷----------------------- 10 def JudeS(choiceS,a): 11 try: 12 if int(choiceS) in range(1,a+1) or int(choiceS) == 1: 13 return False 14 else: 15 return True 16 except: 17 return True 18 #----------------------------------------------新增信息判斷-------------------------- 19 def JudeA(backend,weight,maxconn): 20 try: 21 if backend.startswith("www") and int(weight) in range(50) and int(maxconn) in range(5000): 22 return False 23 except: 24 return True View Code

  5.Deal_order():接受來自Show()的返回值,對輸入的指令進(jìn)行分流給不同的函數(shù)處理。

1 def Deal_order(order,show_list): 2 if order =="Q": #退出 3 pass 4 elif order =="A" : #新建 5 Add() 6 elif order == "D": #刪除 7 Delete(show_list) 8 elif order == "M": #修改 9 Modify(show_list) 10 elif order == "C": 11 Check(show_list) 12 else: 13 order = input("\033[1;31;0m""輸入錯(cuò)誤,請重新輸入:""\033[0m") 14 Deal_order(order,show_list) View Code

  6.Check():查詢,主要查看backend下的server信息。

  效果:

1 #----------------------------------------------查詢--------------------------------- 2 def Check(show_list): 3 choice = input("請選擇backend:") 4 while Jude(choice,show_list): 5 choice = input("\033[1;31;0m""輸入錯(cuò)誤,請重新輸入:""\033[0m") 6 choice = int(choice) 7 print("------------------------------",show_list[choice-1],"------------------------------") 8 for a,b in enumerate(Info[show_list[choice-1]]): 9 print("{} server:{} weight:{} maxconn:{}".format(a+1,b["server"],b["weight"],b["maxconn"])) 10 print("----------------------------------------------------------------------------") 11 select = input("請輸入指令(Q)退出、(B)返回:") 12 while select not in ("Q","B"): 13 select = input("\033[1;31;0m""請輸入正確指令:""\033[0m") 14 if select == "Q": 15 pass 16 elif select == "B": 17 main() View Code

  7.Add():新建,可以新建backend若已存在則在backend下新建server信息,若兩種都存在則修改server信息。

1 #----------------------------------------------增加--------------------------------- 2 def Add(): 3 print("————>請輸入詳細(xì)信息<————") 4 backend = input("Backend名稱:") 5 server = input("Server名稱:") 6 weight = input("權(quán)重(weight):") 7 maxconn = input("最大鏈接數(shù)(maxconn):") 8 while JudeA(backend,weight,maxconn): 9 print("\033[1;31;0m""輸入信息錯(cuò)誤,請重新輸入!!!""\033[0m") 10 Add() 11 if backend not in Info: 12 Info[backend] = [{"server":server,"weight":weight,"maxconn":maxconn}] 13 print(">>>>>新建成功") 14 else: 15 flag = 1 16 for ser in Info[backend]: 17 if server == ser["server"]: 18 flag = 0 19 ser["weight"] = weight 20 ser["maxconn"] = maxconn 21 print(">>>>>backend及server信息已存在,""\033[1;31;0m""server信息已修改""\033[0m") 22 if flag: 23 Info[backend].append({"server":server,"weight":weight,"maxconn":maxconn}) 24 print(">>>>>backend已存在,""\033[1;31;0m""server添加成功""\033[0m") 25 Save_Info(Info) 26 main() View Code

  8.Delete():刪除,可以刪除backend或Server信息。

  效果:

?

1 #----------------------------------------------刪除--------------------------------- 2 def Delete(show_list): 3 choice = input("刪除Backend(B) or Server(S)?:") 4 while(choice not in ("B","S")): 5 choice = input("\033[1;31;0m""請輸入正確的指令:""\033[0m") 6 selection = input("請選擇Backend編號:") 7 while Jude(selection,show_list): 8 selection = input("\033[1;31;0m""請輸入正確的編號:""\033[0m") 9 selection = int(selection) 10 if choice =="B": 11 del Info[show_list[selection-1]] 12 Save_Info(Info) 13 print("\033[1;31;0m""刪除成功!""\033[0m") 14 main() 15 elif choice == "S": 16 print("------------------------------",show_list[selection-1],"------------------------------") 17 for a,b in enumerate(Info[show_list[selection-1]]): 18 print("{} server:{} weight:{} maxconn:{}".format(a+1,b["server"],b["weight"],b["maxconn"])) 19 print("----------------------------------------------------------------------------") 20 choiceS = input("選擇要?jiǎng)h除的Server:") 21 while JudeS(choiceS,a): 22 choiceS = input("\033[1;31;0m""請輸入正確的編號:""\033[0m") 23 choiceS = int(choiceS) 24 Info[show_list[selection-1]].pop(choiceS-1) 25 Save_Info(Info) 26 print("\033[1;31;0m""刪除成功!""\033[0m") 27 main() View Code

  9.Modify():修改,修改backend或是Server信息。

  效果:

?

1 def Modify(show_list): 2 selection = input("修改backend(B) or server(S)?>") 3 while (selection not in ("B","S")): 4 selection = input("\033[1;31;0m""請輸入正確回答:""\033[0m") 5 choice = input("請輸入要修改的backend編號:") 6 while Jude(choice,show_list): 7 choice = input("\033[1;31;0m""請輸入正確編號:""\033[0m") 8 choice = int(choice) 9 if selection == "B": 10 backend = input("修改backend名稱為:") 11 while not backend.startswith("www"): 12 backend = input("\033[1;31;0m""請輸入合法的backend名稱:""\033[0m") 13 temp = Info[show_list[choice-1]] 14 del Info[show_list[choice-1]] 15 Info[backend] = temp 16 Save_Info(Info) 17 print("\033[1;31;0m""修改backend成功""\033[0m") 18 main() 19 else: 20 print("------------------------------",show_list[choice-1],"------------------------------") 21 for a,b in enumerate(Info[show_list[choice-1]]): 22 print("{} server:{} weight:{} maxconn:{}".format(a+1,b["server"],b["weight"],b["maxconn"])) 23 print("----------------------------------------------------------------------------") 24 selection = input("修改哪個(gè)server?>") 25 while JudeS(selection,a): 26 selection = input("\033[1;31;0m""請輸入正確的server編號:""\033[0m") 27 selection = int(selection) 28 sername = input("修改server為:") 29 Info[show_list[choice-1]][a-1]["server"] = sername 30 Save_Info(Info) 31 print("\033[1;31;0m""修改server成功""\033[0m") 32 main() View Code

?

?

?

  

?

轉(zhuǎn)載于:https://www.cnblogs.com/AngelYuFan/p/6752460.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Python蜕变-2017-4-23的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。