Python基于nginx访问日志并统计IP访问量
生活随笔
收集整理的這篇文章主要介紹了
Python基于nginx访问日志并统计IP访问量
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
? ? 如果想看看Nginx有多少IP訪問量,有哪些國家訪問,并顯示IP地址的歸屬地分布,python可以結(jié)合使用高春輝老師ipip.net【免費(fèi)版 IP 地址數(shù)據(jù)庫】,Shell可以使用nali,我這邊主要使用python語言來實(shí)現(xiàn)需求,并將查詢結(jié)果以郵件形式發(fā)送,也是為了學(xué)習(xí)和回顧python語言。很感謝高春輝老師提供的免費(fèi)版IP地址數(shù)據(jù)庫。
一、Ningx日志如下:
二、下載?免費(fèi)版 IP 地址數(shù)據(jù)庫
?#wget??http://s.qdcdn.com/17mon/17monipdb.zip#unzip??17monipdb.zip三、IP庫常見問題FAQ
?示例代碼:
import?os from?ipip?import?IP from?ipip?import?IPXIP.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")?執(zhí)行輸出:
中國??天津??天津??????鵬博士 中國??天津??天津??????鵬博士???39.128399???117.185112??Asia/Shanghai???UTC+8???120000?IP庫guihub地址:https://github.com/17mon/python
四、Python 統(tǒng)計(jì)代碼
#encoding=utf8import?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=0views=views+1ret[ip]=viewsreturn?retdef?run():ip_views=stat_ip_views(nginx_log_path)max_ip_view={}fileName='out.csv'f=open('out.csv','w+')b?=?'IP,國家,訪問數(shù)總數(shù)'print?>>?f,bfor?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,cif?len(max_ip_view)==0:max_ip_view[ip]=viewselse:_ip=max_ip_view.keys()[0]_views=max_ip_view[_ip]if?views>_views:max_ip_view[ip]=viewsmax_ip_view.pop(_ip)print?"IP:",?ip,?"國家:",?countery,?"訪問數(shù):",?views?print?"總共有多少IP:",?len(ip_views)print?"最大訪問IP數(shù):",?max_ip_viewg?=?""d?=?'總共有多少IP:%s'?%(len(ip_views))e?=?'最大訪問IP數(shù):%s'?%(max_ip_view)print?>>?f,gprint?>>?f,dprint?>>?f,edef?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_addressmsg['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>? ?附件內(nèi)國家IP訪問數(shù)據(jù)分析統(tǒng)計(jì),請查收!??<br>? ?如有任何問題,請及時(shí)與我聯(lián)系!'Subject?=?'[分析]國家創(chuàng)建數(shù)據(jù)IP分析統(tǒng)計(jì)'sendMail(html=Content,emailaddress='kuangl@test.com',mailSubject=Subject)五、執(zhí)行結(jié)果
utf-8 IP:?41.42.97.104?國家:?埃及?訪問數(shù):?2 IP:?99.122.189.203?國家:?美國?訪問數(shù):?3 總共有多少IP:?2 最大訪問IP數(shù):?{'99.122.189.203':?3}六、郵件發(fā)送結(jié)果
轉(zhuǎn)載于:https://blog.51cto.com/kling/1615505
總結(jié)
以上是生活随笔為你收集整理的Python基于nginx访问日志并统计IP访问量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【解决方法】登录 Ansys 官网查看官
- 下一篇: python xml.dom模块解析x