Python_程序练习1_文件

Posted soapolddaddy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python_程序练习1_文件相关的知识,希望对你有一定的参考价值。

程序:修改haproxy配置文件 

需求:

 

1、查
    输入:www.oldboy.org
    获取当前backend下的所有记录

2、新建
    输入:
        arg = {
            ‘bakend‘: ‘www.oldboy.org‘,
            ‘record‘:{
                ‘server‘: ‘100.1.7.9‘,
                ‘weight‘: 20,
                ‘maxconn‘: 30
            }
        }

3、删除
    输入:
        arg = {
            ‘bakend‘: ‘www.oldboy.org‘,
            ‘record‘:{
                ‘server‘: ‘100.1.7.9‘,
                ‘weight‘: 20,
                ‘maxconn‘: 30
            }
        }

  

技术分享图片
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.oldboy.org
        server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000
原配置文件

 

作业代码:

# Author:soap

menu = {
    ‘search‘,
    ‘add‘,
    ‘delete‘,
    ‘exit‘
}
exit_flag = 1
while exit_flag:
        for k in menu:
            print (k)
        chioce = input(‘>>:‘)
        if chioce == ‘search‘:
            with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as file:
                search_info = ‘backend ‘ + input(‘please input the web site: ‘)
                for line in file:
                    if search_info in line:
                        print(file.readline())
        elif chioce == ‘add‘:
            with open(‘haproxy‘, ‘a+‘, encoding=‘utf-8‘) as file:
                add_info = eval(input(‘plaese input the web site info: ‘))
                write_info_1 = ‘backend ‘ + str(add_info[‘backend‘]) + ‘\n‘
                write_info_2 = ‘        ‘ + ‘server ‘ + str(add_info[‘record‘][‘server‘]) + ‘ weight ‘ +                     str(add_info[‘record‘][‘weight‘]) + ‘ maxconn ‘ + str(add_info[‘record‘][‘maxconn‘]) + ‘\n‘
                file.write(write_info_1)
                file.write(write_info_2)
        elif chioce == ‘delete‘:
            with open(‘haproxy‘, ‘r+‘, encoding=‘utf-8‘) as file:
                delete_info = eval(input(‘plaese input the web site info: ‘))
                find_str_1 = ‘backend ‘ + delete_info[‘backend‘]
                find_str_2 = ‘server ‘ + delete_info[‘record‘][‘server‘]
                for line in file:
                    if find_str_1 in line:
                        continue
                    if find_str_2 in line:
                        continue
                    file.write(line)
        elif chioce == ‘exit‘:
            exit_flag = 0

  

以上是关于Python_程序练习1_文件的主要内容,如果未能解决你的问题,请参考以下文章

python路5__购物车小程序练习

Python入门练习_登录

Python json练习_读写文件函数

python 学习_基础语法__练习

python中的模块,库,包有啥区别

Python练习-装饰器版-为什么我的用户总被锁定