python统计独立IP

2023-01-31 01:01:41 python 统计 独立

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

#!/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)


相关文章