python刷新Squid和TrafficServer脚本,squidtrafficserver,cacherefreas


cacherefreash.py

#!/usr/local/bin/python3#_*_ coding:utf8 _*_import syssys.path.insert(0, "/usr/local/python3/lib")import cgiimport cgitbcgitb.enable()#scrip for squid & apache traffic server refreashingfrom http.client import HTTPConnectionimport threadingsquid = ['1.1.1.1','1.1.1.2','1.1.1.3','1.1.1.4'] ##此处为服务器IP地址ats = ['2.2.2.1','2.2.2.2','2.2.2.3']   ##此处为服务器IP地址http_headers = {         "Host" : "CacheRefresher",         "User-Agent" : "SquidClient",        "Accept" : "*/*",        "Accept-Encoding" : "gzip,deflate",}def cachepurge(ips,ulist):    conn = HTTPConnection(ips)    conn.request('PURGE', ulist, headers = http_headers)  # PURGE 方法。    t_status = conn.getresponse() # 存为常量,方便操作    p_status = t_status.status  # 取出HTTP代码,存为常量,连接外打印    r_status = t_status.reason  # 取出HTTP代码说明,存为常量,连接外打印    conn.close()    conn = HTTPConnection(ips)    conn.request('GET', ulist, headers = http_headers)    a_status = conn.getresponse()    h_status = a_status.getheader('Age') # 取出HTTP header中的file Age,存为常量,连接外打印    conn.close()    print ('<tr>')    print ("<td> SERVER %s </td>" % ips)    print('<td>',p_status,r_status,'</td>')    print("<td> FILE AGE: %s</td>" % h_status)    print ('</tr>')def multiconn(arg_a,arg_b,arg_c):    threads = []    for i in arg_b:        t = threading.Thread(target=arg_a, args=(i,arg_c))        threads.append(t)    for t in threads:        t.start()    for t in threads:        t.join()# Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fieldsif form.getvalue('textcontent'):    text_t = form.getvalue('textcontent')    text_content = text_t.split('\r\n')else:    text_content = "No input"print ("Content-type:text/html;charset=utf-8\n\n")print ("<html>")print ("<head>")print ("<title>CACHE REFRESHING</title>")print ("</head>")print ("<body>")print ('<table border="1">')for text_c in text_content:    print ('<tr>')    print ("<th>URL</th>")    print ("<th> %s</th>" % text_c)    print ("<th> FILESTATUS</th>")    print ('</tr>')    multiconn(cachepurge,ats,text_c)    multiconn(cachepurge,squid,text_c)#print ('</tr>')print ('</table>')print ("</body>")print ("</html>")

页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-type" content="text/html; charset=utf-8" /><title>CACHE REFRESHING</title></head><body><form action="cgi-bin/cacherefreash.py" method="post"></br>Type your URLs LIST below...</br><textarea name="textcontent" cols="80" rows="8"></textarea></br><input type="submit" value="REFRESH" /><input type="reset" value="RESET" /></form></body></html>

评论关闭