Simple process
Loop output 3 operation options read, modify, delete
Input 1 2 3
After you have entered the number and then execute the appropriate method
Import OS
def read ():
Read
WithOpen"Haproxy.txt","R",encoding="Utf-8")As R1:
list1= R1.readlines ()
For lineIn List1:
If"Backend"In line:
List1_index=list1.index (line) +1
Break
List2=list1[list1_index:]
For Line_newIn List2:
Print (line_new)
# def crtate (bakend,server,weight,maxconn):
def crtate (*args):
Create
WithOpen"Haproxy.txt","A +",encoding="Utf_8")As A1:
arg = {
' Bakend ': args[0],
' Record ': {
' Server ': args[1],
' Weight ': args[2],
' Maxconn ': args[3]
}
}
PrintType (ARG))
For IIn ARG:
Print (I,arg[i])
If i=="Record":
A1.write ("\n\t\t"+STR (Arg.get ("Record")). Replace ("‘",""). Replace ("{"," ").
Replace"}",""). Replace (":",""))
Else
A1.write ("\ n"+STR (i))
A1.write (" "+STR (arg[i]))
A1.flush ()
def delete (x):
Delete
WithOpen"Haproxy.txt","R",encoding="Utf-8")As D1, \
Open"Haproxy_new.txt","W",encoding="Utf-8")As D2:
D1a=d1.readlines ()
For Index,lineInchEnumerate (D1A):
If XIn line:
Print (index)
Delete_index=index
Print (D1a[delete_index])
Print (d1a[delete_index+1])
# d1.seek (0)
D1a.pop (Delete_index)
D1a.pop (Delete_index)
For IIn D1A:
D2.write (i)
Os.rename ("Haproxy.txt","Haproxy.bak")
Os.rename ("Haproxy_new.txt","Haproxy.txt")
While True:
q=InputPlease enter the action number 1. Read 2. Add 3. Delete >> ")
If q=="1":
Read ()
if q== "2":
W =input ( "first >>>:")
W1 = input ( "second >>>:")
W2 = Span style= "COLOR: #000080" >input ( "third >>>:")
W3 = Span style= "COLOR: #000080" >input ( "fourth >>>:")
print (W,W1,W2,W3)
Crtate (W,W1,W2,W3)
if q== "3":
S =input ( "Enter a parameter >>>")
Delete (s)
Python Learning Notes--Modify the configuration file by three