练习目的:
1.函数编程
2.文件处理
3.tag的用法
4.程序的解耦
文件内容:haproxy.conf
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 101.1000.7.9 101.1000.7.9 weight 20 maxconn 30
server 2.2.2.7 2.2.2.7 weight 30 maxconn 4000
server 10.10.10.1 10.10.10.1 weight 22 maxconn 2000
server 2.2.2.5 2.2.2.5 weight 30 maxconn 4000
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 33333333333
代码内容:
import os
def file_handler(backend_data,res=None,type='fetch'):
if type == 'fetch':
with open('haproxy.conf','r') as read_f:
tag=False
ret=[]
for read_line in read_f:
if read_line.strip() == backend_data:
tag=True
continue
if tag and read_line.startswith('backend'):
# tag=False
break
if tag:
print('