File Operation Exercise: http://www.cnblogs.com/wupeiqi/articles/4950799.html
Global Log127.0.0.1local2 Daemon Maxconn256Log127.0.0.1local2 infodefaults Log Global mode HTTP timeout connect 5000ms timeout client 50000ms Timeout server 50000ms option Dontlognulllisten stats:8888stats enable stats uri/admin Stats Auth admin:1234frontend oldboy.org Bind0.0.0.0:80option httplog option httpclose option forwardfor log global ACL www hdr_reg (host )-I www.oldboy.org use_backend www.oldboy.org if wwwbackend www.oldboy.org server100.1.7.9 100.1.7.9 Weight Maxconn 3000Backend buy.oldboy.org Server100.1.7.90 100.1.7.90 Weight Maxconn 3000Copy Code
Requirements:
Output:1, get Ha Records2, increase Ha records3, delete the HA record num=raw_input (' Please enter operation number: ') If the user enters the1: Read=raw_input (' Please enter backend: ') such as input: www.oldboy.org The configuration file backend all records under the Www.oldboy.org node are acquired and entered into the terminal if the user enters the2: Read= Raw_input (' Please enter the record to be added: ') such as: {"backend": "test.oldboy.org", "record": {"Server": "100.1.7.9", "Weight": $, "maxconn": 30}} # to convert the read string to a dictionary type read_dict=json.loads (read) Backend_title= Read_dict[' backend ']# go to config file to find the specified node: # backend Test.oldboy.org # If it already exists, # then add a record under this node based on the input, for example: Server 100.1.7.9 100.1.7.9 Weight Maxconn 3000# If it does not exist, add the backend node and record, for example: backend test.oldboy.org server100.1.7.9 100.1.7.9 Weight Maxconn 3000# (optional) You can then determine whether the record under the node already exists if the user entered the3: Read= Raw_input (' Enter the record to delete: ') such as: {"backend": "test.oldboy.org", "record": {"Server": "100.1.7.9", "Weight": $, "maxconn": 30}} # to convert the read string to a dictionary type read_dict=json.loads (read) Backend_title= Read_dict[' backend ']#去配置文件中找到指定的节点, and delete the specified records, such as: backend test.oldboy.org server100.1.7.10 100.1.7.10 Weight Maxconn 3000Server100.1.7.9 100.1.7.9 Weight Maxconn 3000# Delete # (optional) If all the records under backend have been deleted, the current backend test.oldboy.org is removed.
Reference ideas:
Http://www.cnblogs.com/wupeiqi/articles/5539371.html
#read a row linef=open (XXX) forLineinchF:Print(line)defFetch (BACKEND): Result=[] with open ("ha.conf",'R', encoding="Utf-8") as F:flag=False forLineinchF:ifLine.strip (). Startwith ("Backend") andLine.strip () = ="Backend"+Backend Flag=TrueContinue ifFlag==true andLine.strip (). Startwith ("Backend"): Flag=False Break ifFlag andLine.strip (): Result.append (Line.strip ())returnresultdefAdd (Backend,record):#idea One:#Check if the record exists firstrecord_list=Fetch (BACKEND)if notrecord_list:#backend does not existWith open ('ha.conf', R) as Old,open ("new.conf",'W') as NEW: forLineinchOld:new.write (line) New.write ("Backend"+backend +"\ n") New.write (" "* 8 + record +"\ n") Else: #Backend exists #If the record exists, do not do anything, add the record to backend if it does not exist ifRecordinchrecord_list:#Record exists Importshutil shutil.copy ("ha.conf","new.conf") Pass Else: #backend exists, record does not existRecord_list.append (record) with open ('ha.conf', R) as Old,open ("new.conf",'W') as New:flag=False forLineinchOld :ifLine.strip (). Startwith ("Backend") andLine.strip () = ="Backend"+Backend:flag=True New.write (line) forNew_lineinchRecord_list:new.write (" "*8 + New_line +"\ n") ifFlag andLine.strip (). Startwith ("Backend") Flag=False New.write (line)Continue ifLine.strip () and notFlag:new.write (line) BK='test.oldboy.org'Rd="server 100.1.7.10 100.1.7.10 weight maxconn"Add (BK,RD)#idea two:s="[11,22,33,44,55]" #the representation of the dictionary is s = ' {' K1 ': ' v1 '} ', inside the element if it is a string, be sure to use double quotation marksPrint(Type (s), s)ImportJsonn= Json.loads (s)#converts a string to the data type of Python, [],{}Print(Type (n), N)ImportJsonr= Input ("Input:") DiC=Json.loads (r) BK=dic['Beckend']rd="Server%s%s weight%d Maxconn%d"% (dic['Record'][server],dic['Record'][server],dic['Record']['Weight'],dic['Record']['Maxconn'])
python-file operation Exercise-configuration file read, modify, delete