首页 > 其他 > 详细

第三节课作业(文件修改在,增加,查看)

时间:2017-10-24 10:17:22      阅读:282      评论:0      收藏:0      [点我收藏+]
import sys
def lookup(): ##查看
    with open(‘haproxy‘,‘r+‘,encoding=‘utf-8‘) as f:
        line = input(‘Please enter the lookup domain name:‘)
        for linef in f:
            if line in linef:
                print(linef)
                print(f.readline())
            else:
                continue
def dell(): #删除前先查找
    with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as f:
        delle = input("Please enter the domain name to delete:")
        for linef in f:
            if delle in linef:
                return linef,f.readline()
            else:
                continue

def increase(bakend,server,weight,maxconn,record=‘record‘):
    out = ‘‘‘
bakend  %s
        server %s weight %s maxconn %s
    ‘‘‘ % (bakend,server,weight,maxconn)
    return (out)

def incres(): ##增加
    bakend = input("Please enter the domain name:")
    with open(‘haproxy‘,‘r+‘,encoding=‘utf-8‘) as f:
        for line in f:
            if bakend in line:
                print("Domain name already exists.")
                sys.exit()
            else:
                continue
    server = input("Please enter the server IP address:")
    weigth = input("Please input weight:")
    maxconn = input("Please input timeout time:")
    x = increase(bakend,server,weigth,maxconn)
    with open(‘haproxy‘,‘a‘,encoding=‘utf-8‘) as f:
        f.write(x)
def dellet():
    with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as f , open(‘haproxy_1‘,‘w‘,encoding=‘utf-8‘) as f_1 :
        x = dell()
        for line in f:
             if line in x:
                 continue
             else:
                 f_1.write(line)

inp = input("Please input what you want to do(Add,delete,view):")
if inp == ‘add‘:
    incres()
elif inp == ‘delete‘:
    dellet()
elif inp == ‘view‘:
    lookup()
else:
    exit()


深圳运维开发交流群 --->>599706738(草创)
 

第三节课作业(文件修改在,增加,查看)

原文:http://www.cnblogs.com/huangbipeng/p/7722091.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!