python统计独立IP

2020-01-13 12:41:14 浏览数 (1)

####python统计独立IP####

代码语言:javascript复制
#!/usr/bin/python
#coding:utf8
import re
import sys
import time
yesterday=time.strftime('%Y%m%d',time.localtime(time.time()-24*60*60))

path='/var/logs/usage.wifigx.com/'

filenamelist={'wifibao':'tj.wifibao.com_tj','wifids':'tj.wifids.com_tj',
                'qqtj':'tj.wifigx.com_qqtj','tj':'tj.wifigx.com_tj',
                'wifiwp':'tj.wifiwp.com_tj'}
                
def uniq_ip_func(filename,yesterday,path):
            contents=path filename   '_'  yesterday   '.log'
            print contents
            #IP:4个字符串,每个1到3个数字,由点连接
            ipadd = r'.'.join([r'd{1,3}']*4)
            re_ip = re.compile(ipadd)
            iphitlisting = {}
            for line in open(contents):
                    match = re_ip.match(line)
                    if match:
                            ip = match.group()
                            #如果IP存在增加1,否则设置点击率为1
                            iphitlisting[ip] = iphitlisting.get(ip, 0)   1
            
            #print len(iphitlisting)
            
            result=yesterday ' ' filename ' ip= ' str(len(iphitlisting)) 'n'
            
            anlyResult=open('/usr/local/nginx/html/tj_all.txt','a')
            anlyResult.write(result)
            anlyResult.close()
            
if __name__ == '__main__':
    for (k,v) in filenamelist.items():
          uniq_ip_func(v,yesterday,path)

0 人点赞