本文共 1745 字,大约阅读时间需要 5 分钟。
__author__ = 'similarface'from collections import defaultdictimport globip = r"?P [\d.]*"date = r"?P \d+"month = r"?P \w+"year = r"?P \d+"log_time = r"?P \S+"method = r"?P \S+"request = r"?P \S+"status = r"?P \d+"bodyBytesSent = r"?P \d+"refer = r"""?P [^\"]* """userAgent=r"""?P .* """import reimport linecachedef readline(path): return linecache.getlines(path)ipad=0result=defaultdict(int)p = re.compile(r"(%s)\ -\ -\ \[(%s)/(%s)/(%s)\:(%s)\ [\S]+\]\ \"(%s)?[\s]?(%s)?.*?\"\ (%s)\ (%s)\ \"(%s)\"\ \"(%s).*?\"" %( ip, date, month, year, log_time, method, request, status, bodyBytesSent, refer, userAgent ), re.VERBOSE)acctype=set()linuxcount=0Androidcount=0shoujidict=defaultdict(int)for line in open('/data4/access.log','r'): m = re.findall(p, line) try: if len(m)!=0: #"Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; SM-G5108Q Build/KTU84P) #(Linux; U; Android 4.4.2; zh-cn; HUAWEI MT7-CL00 Build/HuaweiMT7-CL00) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/02544 result[m[0][10]]=result[m[0][10]]+1 try: content=m[0][10] begin=content.index('(') end=content.index(')') shortcontent=content[begin+1:end] acctype.add(shortcontent.split(';')[0]) #print(shortcontent) if 'Linux' in shortcontent: linuxcount=linuxcount+1 shouji=shortcontent.split(';')[4] shoujidict[shouji]=shoujidict[shouji]+1 if 'Windows' in shortcontent: pass if 'iPad' == shortcontent.split(';')[0]: ipad=ipad+1 except Exception,e: pass except Exception,e: #print(m) passfopen=open('/tmp/shouji.txt','w')for k,v in shoujidict.items(): fopen.writelines(k+'\t'+str(v)+"\n")fopen.close()print(ipad)
转载于:https://www.cnblogs.com/similarface/p/5748231.html