想看Nginx有多少IP视图,浏览了哪些国家,显示IP地址数属于全国各地,python可以应用高春辉老师的ipip.net【绿色IP详细地址数据库查询】,Shell可以应用李娜。我这边主要是应用python语言来满足需求,通过邮件推送结果,以便更好的学习训练和回头看python语言。非常感谢高春辉老师赠送的绿色IP地址数据库查询。
一、Ningx日志如下:
第二,免费下载绿色IP详细地址数据库。
#wget http://s.qdcdn.com/17mon/17monipdb.zip #unzip 17monipdb.zip三,知识产权图书馆常见问题
示例代码:
import os from ipip import IP from ipip import IPX IP.load(os.path.abspath("mydata4vipday2.dat")) print IP.find("118.28.8.8") IPX.load(os.path.abspath("mydata4vipday2.datx")) print IPX.find("118.28.8.8")机具输出:
我国 天津市 天津市 鹏博士 我国 天津市 天津市 鹏博士 39.128399 117.185112 Asia/Shanghai UTC8 120000IP库guihub详细地址:https://github.com/17mon/python
四。Python统计代码
#encoding=utf8 import re,sys,os,csv,smtplib from ipip import IP from ipip import IPX from email import encoders from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from optparse import OptionParser reload(sys) sys.setdefaultencoding('utf-8') print sys.getdefaultencoding() nginx_log_path="/app/nginx/logs/apptest_www.access.log" pattern = re.compile(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}') def stat_ip_views(log_path): ret={} f = open(log_path, "r") for line in f: match = pattern.match(line) if match: ip=match.group(0) if ip in ret: views=ret[ip] else: views=0 views=views1 ret[ip]=views return ret def run(): ip_views=stat_ip_views(nginx_log_path) max_ip_view={} fileName='out.csv' f=open('out.csv','w') b = 'IP,我国,浏览数数量' print >> f,b for ip in ip_views: IP.load(os.path.abspath("17monipdb.dat")) count=IP.find("%s"% (ip)) conut_s=count.split() countery=conut_s[0] views=ip_views[ip] c = '%s,%s,%s' %(ip,countery,views) print >> f,c if len(max_ip_view)==0: max_ip_view[ip]=views else: _ip=max_ip_view.keys()[0] _views=max_ip_view[_ip] if views>_views: max_ip_view[ip]=views max_ip_view.pop(_ip) print "IP:", ip, "我国:", countery, "浏览数:", views print "一共有多少IP:", len(ip_views) print "较大浏览IP数:", max_ip_view g = "" d = '一共有多少IP:%s' %(len(ip_views)) e = '较大浏览IP数:%s' %(max_ip_view) print >> f,g print >> f,d print >> f,e def sendMail(html,emailaddress,mailSubject,from_address="other@test.com"): mail_list=emailaddress.split(",") msg=MIMEMultipart() msg['Accept-Language']='zh-CN' msg['Accept-Charset']= 'ISO-8859-1,utf-8' msg['From']=from_address msg['to']=";".join(mail_list) msg['Subject']=mailSubject.decode("utf-8") txt=MIMEText(html,'html','utf-8') txt.set_charset('utf-8') msg.attach(txt) file=MIMEBase('application', 'octet-stream') file.set_payload(open(fileName, 'rb').read()) encoders.encode_base64(file) file.add_header('Content-Disposition', 'p_w_upload; filename="%s"' % os.path.basename(fileName)) msg.attach(file) smtp=smtplib.SMTP("mail.test.com") smtp.sendmail(msg["From"],mail_list,msg.as_string()) smtp.close() if __name__ == '__main__': run() fileName='out.csv' cmd = 'iconv -f UTF8 -t GB18030 %s -o %s.bak && mv %s.bak %s' %(fileName,fileName,fileName,fileName) os.system(cmd) Content= 'Dear ALL: <br> 配件内我国IP浏览数据统计分析统计分析,请查收! <br> 若有一切难题,请立即和我联络!' Subject = '[剖析]我国建立数据信息IP剖析统计分析' sendMail(html=Content,emailaddress='kuangl@test.com',mailSubject=Subject)动词(verb的缩写)实施结果
utf-8 IP: 41.42.97.104 我国: 印度 浏览数: 2 IP: 99.122.189.203 我国: 英国 浏览数: 3 一共有多少IP: 2 较大浏览IP数: {'99.122.189.203': 3}六、发送邮件结果
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)