haproxy.conf
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.oldboy1.org server 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30 server 2.2.2.7 2.2.2.7 weight 30 maxconn 4000 server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000 server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000 backend www.oldboy2.org server 3.3.3.3 3.3.3.3 weight 20 maxconn 3000 backend www.oldboy20.org server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33333333333
import os
if __name__ == "__main__": # variable
msg = "" "
1: Query
2: Add
3: Modify the
4: Delete
5: Exit
" ""
msf_dic = { "1": FETCH, "2": the Add (), "3": change, "4 ": delete}
True the while:
Print (msg)
Choice = the INPUT ( "Please enter your options:"). Strip ()
IF not Choice: the Continue
IF Choice == "5": BREAK
the Data = the INPUT ( "Please enter your data:" ) .strip ()
##############Inquire####################
def fetch(data): # print("这是查询功能") backend_data = 'backend %s' % data with open("haproxy.conf","r") as read_f: tag = False # tag标识一种状态 ret=[] for read_line in read_f: if read_line.strip() == backend_data: tag=True continue if tag and read_line.startswith("backend"): break if tag: print(read_line,end="") ret.append(read_line) return ret
##############modify#################
DEF Change (Data): Print ( "It is a modification function") Print ( "user input data is", Data) backend = Data [0] [ "backend"] # files among a record backend_data = "backend% s "% backend old_server_record ="% sServer% S% S weight% S The maxconn% S \ n-"% (" "*. 8, Data [0] [" Record "] [" Server "], Data [0] [" Record "] [" Server "], Data [0] [" Record "] [" weight "], Data [0] [" Record "] [" The maxconn "]) new_server_record ="% sServer% S% S weight% S maxconn% s \ n "% ( " "* 8,data[1]["record"]["server"], " "* 8,data[1]["record"]["server"], data[1]["record"]["server"], data[1]["record"]["weight"], data[1]["record"]["maxconn"]) print("用户想要修改的记录是",old_server_record) res=fetch(backend) if not res or old_server_record not in res: return "修改记录不存在" else: index=res.index(old_server_record) res[index]=new_server_record res.insert(0,"%s\n" %backend) with open("haproxy.comf","r") as read_f,\ open("haproxy.comf_new","w") as write_f: tag=False has_write=False for read_line in read_f: if read_line.strip() == backend_data: tag=True continue if tag and read_line.startswith("backend") tag=False if not tag: write_f.write(read_line) else: if not has_write: for record in res: write_f.write(record) has_write=True os.rename("haproxy.conf","haproxy.conf.bak") os.rename("haproxy.conf_new","haproxy.conf") os.remove("haproxy.conf.bak")
########### ########## decoupling and the final version
import os def file_handler(backend_data,res=None,type="fetch"): if type == "fetch": with open("haproxy.conf", "r") as read_f: tag = False ret = [] for read_line in read_f: if read_line.strip() == backend_data: tag = True continue if tag and read_line.startswith("backend"): break if tag: print(read_line, end="") ret.append(read_line) return ret elif type == "change": with open("haproxy.comf","r") as read_f,\ open("haproxy.comf_new","w") as write_f: tag=False has_write=False for read_line in read_f: if read_line.strip() == backend_data: tag=True continue if tag and read_line.startswith("backend"): tag=False if not tag: write_f.write(read_line) else: if not has_write: for record in res: write_f.write(record) = True has_write os.rename ( "haproxy.conf", "haproxy.conf.bak") os.rename ( "haproxy.conf_new", "haproxy.conf") The os.remove ( "haproxy.conf.bak") DEF FETCH (Data): backend = data [0] [ "backend"] # files among a record backend_data = "backend% S"% backend old_server_record = "% sServer% S% S weight% S maxconn% s \ n "% ( " "* 8, data [0] [" record "]["server"], # Print ( "This is the search function") backend_data = 'backend% S'% Data return file_handler are (backend_data) DEF the Add (): Pass DEF Change (Data): Print ( "It is a modification function") Print ( "user input the data ", data) data [0] [" Record "] [" Server "], data [0] [" Record "] [" weight "], data [0] [" Record "] [" the maxconn " ]) new_server_record = "sServer% weight% S% S% S% S The maxconn \ n-"% ( "" *. 8, Data [. 1] [ "Record"] [ "Server"], data[1]["record"]["server"], Data [. 1] [ "Record"] [ "weight"], Data [. 1] [ "Record"] [ "The maxconn"]) Print ( "the user wants to modify record", old_server_record) RES = FETCH (backend) not not old_server_record RES or IF in RES: return "modify record does not exist" the else: index = res.index (old_server_record) res[index]=new_server_record res.insert (0, "% S \ n-" backend%) file_handler are (backend_data, RES = RES, type = "Change") DEF the Delete (): Pass IF __name__ == "__main__": # variable msg = "" " 1: query 2: Add 3:Modify 4: Delete 5: Quit . "" " msf_dic = { "1": FETCH, "2": the Add (), "3": Change, "4": delete} the while True: Print (MSG) Choice = input ( "Please enter your options:") Strip (). IF not Choice: the Continue IF Choice == "5": BREAK the data = input ( "Please enter your data:"). Strip () ! = IF Choice "1": data = eval (data) #eval extract data structure string, the string may be performed an operation expression thereof RES = msf_dic [Choice] (Data) Print (RES)
----------