首页 > 代码库 > Python实战二

Python实战二

  要求:按照要求完成对文件的增、删、改、查操作。

def add(**kwargs):
    ‘‘‘新增内容,在指定位置新增‘‘‘
    while True:
        flag = False
        with open("test",encoding="utf8") as f_read,                open("test1",mode="w",encoding="utf8") as f_write:
            for line in f_read:
                if line.startswith("backend") and kwargs["backend"] in line:#匹配URL行
                    flag = True #设定开关
                if kwargs["record"]["server"] in line                             and kwargs["record"]["weight"] in line                             and kwargs["record"]["maxconn"] in line                             and flag == True:#匹配服务器里详细信息位置
                    f_write.write(line)#写入原来的信息
                    f_write.write("\t\tserver %s %s weight %s maxconn %s"%                                  (kwargs["record1"]["server"],kwargs["record1"]["server"],                                   kwargs["record1"]["weight"],kwargs["record1"]["maxconn"]))#这是新增的内容
                    flag = False #合上开关
                    continue
                f_write.write(line)#继续写其他的内容

def delete(**kwargs):
    ‘‘‘删除指定位置的文件‘‘‘
    while True:
        flag = False
        with open("test",encoding="utf8") as f_read,                open("test1",mode="w",encoding="utf8") as f_write:
            for line in f_read:
                if line.startswith("backend") and kwargs["backend"] in line :#匹配URL行
                    flag = True #设定开关
                if kwargs["record"]["server"] in line                         and kwargs["record"]["weight"] in line                         and kwargs["record"]["maxconn"] in line                         and flag == True:#这是删除行的信息
                        flag = False
                        continue #跳过要删除的行,不写入
                f_write.write(line)#继续写入其他的内容

def change(**kwargs):
    ‘‘‘修改指定位置的文件‘‘‘
    while True:
        flag = False
        with open("test", encoding="utf8") as f_read,                 open("test1", mode="w", encoding="utf8") as f_write:
            for line in f_read:
                if line.startswith("backend") and kwargs["backend"] in line:#匹配URL行
                    flag = True
                if kwargs["record"]["server"] in line                         and kwargs["record"]["weight"] in line                         and kwargs["record"]["maxconn"] in line                         and flag == True:#定位到指定的位置
                    f_write.write("\t\tserver %s %s weight %s maxconn %s"%                              (kwargs["record1"]["server"],kwargs["record1"]["server"],                               kwargs["record1"]["weight"],kwargs["record1"]["maxconn"]))#写入修改的信息
                    flag = False
                    continue#跳出本次循环,继续下次循环
                f_write.write(line)#继续写入其他的内容

def query(choice):
    ‘‘‘查询想要的内容‘‘‘
    while True:
        flag = False
        with open("test",encoding="utf8") as f_read,                open("test1",mode="w",encoding="utf8") as f_write:
            for line in f_read:
                if line.startswith("backend") and choice in line :#匹配URL行信息
                    flag = True #设个开关
                    continue #跳出本次,继续下次
                if line.startswith("backend") and flag :#匹配结束行的位置
                    flag = False #合上开关
                    break #退出循环
                if flag:
                    f_write.write(line) #写入刚刚匹配到的内容,就完成了查询的目的

def tell_msg():
    ‘‘‘用户输入提示‘‘‘
    msg = ‘‘‘
        1   新增
        2   删除
        3   修改
        4   查询
        ‘‘‘
    print(msg)

cmd_dic = {#该子典根据用户输入的数字,也就是字典的key,调取key得到对应的value
    "1":add,
    "2":delete,
    "3":change,
    "4":query
}

while True:
    tell_msg()#调用输入提示函数
    choice = input("please input choices:").strip()#输入要操作的数
    if choice  == "1" or choice  == "3":#因为新增和修改要输入位置参数和要变更的参数,so,要输入两个信息
        choice1 =eval(input("请以字典形式输入位置参数:"))
        choice2 =eval(input("请以字典形式输入变更参数:"))
        cmd_dic[choice](**choice1,**choice2)#通过用户输入的数,调取字典里的value用作函数名,调用相关函数
        continue
    if choice == "2" :
        choice1 =eval(input("请以字典形式输入位置参数:"))
        cmd_dic[choice](**choice1)
        continue
    if choice == "4":
        choice1 = input("请输入域名参数:")
        cmd_dic[choice](choice1)
        continue
    if choice == "q":#退出程序
        print("ByeBye!")
        break
    else:#输入不对时,提示,继续循环
        print("Do not have this option!")
        continue

  当用户选择“1”时,用户输入的信息为:

{"backend": "www.oldboy20.org", "record": {"server": "10.10.0.10 10.10.0.10", "weight": "9999", "maxconn": "33"}}#输入的位置参数
{"record1":{"server":"1.1.1.1","weight":"2222","maxconn":"3333"}}#这是要新增的参数
------------------------以下是新增后的文件-----------------------
backend www.oldboy20.org
        server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
	  server 1.1.1.1 1.1.1.1 weight 2222 maxconn 3333

  当用户选择“2”时,用户输入的信息为:

{"backend":"www.oldboy1.org","record":{"server":"10.10.0.10 10.10.0.10","weight":"9999","maxconn":"33"}}#输入要删除的信息
------------------------以下删除后的文件-------------------------
backend www.oldboy1.org
        server 10.10.10.1 10.10.10.1 weight 22   maxconn 2000
        server 2.2.2.4 2.2.2.4       weight 20   maxconn 3000

  当用户选择“3”时,用户输入的信息为:

{"backend":"www.oldboy2.org","record":{"server":"3.3.3.3 3.3.3.3","weight":"20","maxconn":"3000"}}#位置参数信息
{"record1": {"server":"0000","weight":"0000", "maxconn":"0000"}}#要修改的参数信息
--------------------------以下是修改后的文件-------------------------
backend www.oldboy2.org
	server 0000 0000 weight 0000 maxconn 0000

  当用户选择“4”时,用户输入的信息为:

www.oldboy1.org #输入要查询的url地址
-------------------以下是查询的结果文件----------------------
        server 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
        server 10.10.10.1 10.10.10.1 weight 22   maxconn 2000
        server 2.2.2.4 2.2.2.4       weight 20   maxconn 3000

  最后附上test原始文件:

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 10.10.0.10 10.10.0.10 weight 9999 maxconn 33
        server 10.10.10.1 10.10.10.1 weight 22   maxconn 2000
        server 2.2.2.4 2.2.2.4       weight 20   maxconn 3000

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 33

  

 

Python实战二