根据代码猜功能-。-
from jinja2 import Template
#f = open("squid.conf","r")
flag = ""
hostnames = []
backends = []
dict = {}
with open('squid.conf') as file:
for line in file:
print line
if "cache_peer_domain" in line:
array = line.split(" ")
if array[1] != flag:
print array[3]
hostnames.append({"name":array[3].split(".")[1],"host":array[1]})
flag = array[1]
dict[array[1]] = array[3].split(".")[1]
for a in array:
print a.strip("\n\r")
if a != "" and a != "cache_peer_domain":
backends.append({"backend":dict[array[1]],"domain":a})
print hostnames
template = Template('''
{% for hostname in hostnames %}
backend {{ hostname.name }} {
.host = "{{ hostname.host }}";
.port = "80";
}
{% endfor %}
sub vcl_recv {
{% for backend in backends %}
if (req.http.host ~ "{{ backend.domain }}") {
set req.backend_hint = {{ backend.backend }};
}
{% endfor %}
}
''')
result = template.render(hostnames=hostnames,backends=backends)
f = open("varnish","w+")
f.write(result)