接上一篇的日誌格式,咱們已經篩選出了全部的IP地址、作法稍有不一樣。nginx正則把IP寫進文件裏、仍是那句話數據太大咱們不能寫進列表裏邊:python
def getIP(): f = open(log,'r') IPdic={} for logline in f.readlines(): matchs = p.match(logline) if matchs !=None: allGroups =matchs.groups() IP=allGroups[0] f1 = open('/tmp/ip.txt','a') f1.write("%s\n"%(IP)) f1.close() f.close()
二、調用IP模塊進行,操做。能夠用Pip安裝。模塊介紹:https://github.com/lxyu/17monip:nginx
pip install 17monip
三、模塊使用。git
[root@monitor python]# cat query.py #!/usr/bin/env python #-*- coding: utf-8 -*- import IP def CheckIP(): outip = open('/tmp/ip.txt','r') output = open('/tmp/output.txt','w') for line in outip: listIP=line.strip('\n') local = IP.find(listIP).strip(" ") if local != "" : output.write(local.encode('utf-8')) output.write('\n') output.close() outip.close() if __name__=="__main__": CheckIP()
四、在生成文件,用中文寫入字典,標記出來,有個地方須要注意的是,當咱們字典使用中文的適合,要用json的方式導出數據:
github
[root@monitor python]# cat queryadd.py #!/usr/bin/env python #coding=utf-8 import json def GetAddress(): pvdic={} f = open('/tmp/output.txt','r') n = 0 for line in f: add = line.split() address=add[1] pvdic[address]=pvdic.get(address,0) +1 pvdic=sorted(pvdic.iteritems(),key=lambda c:c[1],reverse=True) return json.dumps(pvdic,encoding="utf-8",ensure_ascii=False) f.close() if __name__=='__main__': print GetAddress()
五、生成效果:json
[root@monitor python]# python queryadd.py [["廣東", 86], ["河北", 81], ["河南", 78], ["山東", 43], ["浙江", 43], ["福建", 31], ["北京", 30], ["江蘇", 26], ["廣西", 26], ["湖南", 23], ["四川", 21], ["上海", 20], ["天津", 16], ["安徽", 14], ["黑龍江", 13], ["陝西", 11], ["湖北", 8], ["江西", 5], ["雲南", 3], ["吉林", 3], ["山西", 3], ["貴州", 2], ["遼寧", 2], ["甘肅", 1], ["內蒙古", 1], ["重慶", 1], ["香港", 1]]