作业-haproxy配置文件的增删查(有一个bug不知道咋改)

# yangqiao

#查询
'''
f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8")
haproxy_list = f.readlines()
website_address = input("请输入要查询的网址:")
website_address = "backend " + website_address
lenth=len(haproxy_list)
#print(website_address)
i=0
j=1
for line in haproxy_list:
    if website_address in line and line.startswith("backend"):
        print(haproxy_list[i])
        j=i+1
        while "backend" not in haproxy_list[j] :
           print(haproxy_list[j])
           j+=1
           if j >= lenth:
               break
    elif j == lenth and i == lenth:
        print("你输入的内容查询不到")
    i+=1
if i == lenth and j==1:
    print("你输入的内容查询不到")
f.close()



#删除
f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8")
f_new=open("C:\\aaaaaaaaaaaaa\\haproxy_new.txt", "a+", encoding="utf-8")
website_address = input("请输入要删除的网址:")
website_address=eval(website_address)
print(website_address)

for line in f:
    if website_address["bakend"] in line and line.startswith("backend"):
        pass
    elif website_address["record"]["server"] in line:
        pass
    else:
        f_new.write(line)

f.close()
f_new.close()
'''


#新增
f=open("C:\\aaaaaaaaaaaaa\\haproxy.txt", "r", encoding="utf-8")
f_new=open("C:\\aaaaaaaaaaaaa\\haproxy_new.txt", "a+", encoding="utf-8")
haproxy_list = f.readlines()
temple=[]
print(haproxy_list)
lenth=len(haproxy_list)
website_address = input("请输入要新增的网址:")
website_address=eval(website_address)
print("lenth:",lenth)
i=0
j=0
k=0
for line in haproxy_list:
    if website_address["backend"] in line and line.startswith("backend") :
        #print(haproxy_list[i])
        j=i+1
        print(j)
        print(haproxy_list[j])
        #while "backend" not in haproxy_list[j] and j <= lenth-1:
        while j <= lenth-1:
            if  "backend" not in haproxy_list[j]:
                if website_address["record"]["server"] in haproxy_list[j]:
                    j+=1
                else:
                    temple.append(haproxy_list[j])
                    k+=1
                    j+=1
                    #print("ddddddddddddd")
        i=j
        #print(temple)
    elif i <= lenth-1:

        f_new.write(haproxy_list[i])

        if j==0 and i== lenth-1 :
            f_new.write("backend "+website_address["backend"]+"\n")
            f_new.write("        " + "server " + website_address["record"]["server"] + " weight " + website_address["record"][   "weight"] + " maxconn " + website_address["record"]["maxconn"] + "\n")
        i += 1


print(temple)
f.close()
f_new.close()


lenth_temple=len(temple)
if lenth_temple != 0:

    with open("C:\\aaaaaaaaaaaaa\\haproxy_new.txt", "a+", encoding="utf-8") as f_w:
        sss="backend "+website_address["backend"]+"\n"
        f_w.write(sss)
        for line in temple:
            f_w.write(line)
        zzz=str("        "+"server "+website_address["record"]["server"]+" weight "+website_address["record"]["weight"]+" maxconn "+website_address["record"]["maxconn"]+"\n")
        f_w.write(zzz)

 Traceback (most recent call last):
  File "C:/Users/lcl/PycharmProjects/untitled/day1/修改haproxy配置文件.py", line 105, in <module>
    zzz=str("        "+"server "+website_address["record"]["server"]+" weight "+website_address["record"]["weight"]+" maxconn "+website_address["record"]["maxconn"]+"\n")
TypeError: must be str, not int

猜你喜欢

转载自www.cnblogs.com/nickkkk/p/8991333.html