python字串實戰

來源:互聯網
上載者:User

標籤:back   list   gnu   lob   min   info   end   als   pass   

haproxy設定檔

思路:讀一行,寫一行

global        log 127.0.0.1 local2        daemon        maxconn 256        log 127.0.0.1 local2 infodefaults        log global        mode http        timeout connect 5000ms        timeout client 50000ms        timeout server 50000ms        option  dontlognulllisten stats :8888        stats enable        stats uri       /admin        stats auth      admin:1234frontend 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 wwwbackend www.oldboy.org        server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000        backend buy.oldboy.org        server 100.1.7.90 100.1.7.90 weight 20 maxconn 3000設定檔

要求可以提取出指定的backend內容,也可以添加指定backend內容

#!/usr/bin/env python# -*- coding:utf-8 -*-  def fetch(backend):    result = []    with open(‘ha.conf‘, ‘r‘) as f:        flag = False        for line in f:            if line.strip().startswith(‘backend‘) and line.strip() == "backend " + backend:                flag = True                continue            if flag and line.strip().startswith(‘backend‘):                break            if flag and line.strip():                result.append(line.strip())     return result  def add(backend, record):    result = fetch(backend)    if not result:        # 無backend,無record        pass    else:        # 有backend        if record in result:            # 記錄record            pass        else:            result.append(record)            with open(‘ha.conf‘, ‘r‘) as old, open(‘new.conf‘, ‘w‘) as new:                continue_flag = False                for line in old:                     if line.strip().startswith(‘backend‘) and line.strip() == "backend " + backend:                        continue_flag = True                        new.write(line)                        for temp in result:                            new.write(" "*8 + temp + "\n")                        continue                     if continue_flag and line.strip().startswith(‘backend‘):                        continue_flag = False                     if continue_flag:                        pass                    else:                        new.write(line)  def add2(backend, record):    with open(‘ha.conf‘, ‘r‘) as old, open(‘new.conf‘, ‘w‘) as new:        in_backend = False        has_backend = False        has_record = False        for line in old:            if line.strip().startswith(‘backend‘) and line.strip() == "backend " + backend:                has_backend = True                in_backend = True                new.write(line)                continue             if in_backend and line.strip().startswith(‘backend‘):                if not has_record:                    new.write(" "*8 + record + ‘\n‘)                new.write(line)                in_backend = False                continue             if in_backend and line.strip() == record:                has_record = True                new.write(line)                continue             if line.strip():                new.write(line)         if not has_backend:            # 寫backend,寫record            new.write(‘backend ‘+ backend + ‘\n‘)            new.write(‘ ‘*8 + record + ‘\n‘)  # ret = fetch("www.oldboy.org")# print(ret) # add(‘www.oldboy.org‘, "server 100.1.7.10 100.1.7.10 weight 20 maxconn 3000")# add2(‘www.oldboy.org‘, "server 100.1.7.11 100.1.7.11 weight 20 maxconn 3000")

  

python字串實戰

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.