python寫(xiě)的一個(gè)squid訪問(wèn)日志分析的小程序
這兩周組里面幾位想學(xué)習(xí)python,于是我們就創(chuàng)建了一個(gè)這樣的環(huán)境和氛圍來(lái)給大家學(xué)習(xí)。
昨天在群里,貼了一個(gè)需求,就是統(tǒng)計(jì)squid訪問(wèn)日志中ip 訪問(wèn)數(shù)和url的訪問(wèn)數(shù)并排序,不少同學(xué)都大體實(shí)現(xiàn)了相應(yīng)的功能,我把我簡(jiǎn)單實(shí)現(xiàn)的貼出來(lái),歡迎拍磚:
日志格式如下:
%ts.%03tu %6tr %{X-Forwarded-For}>h %Ss/%03Hs %<st %rm %ru %un %Sh/%<A %mt "%{Referer}>h" "%{User-Agent}>h" %{Cookie}>h
1372776321.285 0 100.64.19.225 TCP_HIT/200 8560 GET http://img1.jb51.net/games/0908/19/1549401_3_80x100.jpg - NONE/- image/jpeg "http://www.dhdzp.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQDownload 734; .NET4.0C; .NET CLR 2.0.50727)" pcsuv=0;%20pcuvdata=lastAccessTime=1372776317582;%20u4ad=33480hn;%20c=14arynt;%20uf=1372776310453
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from optparse import OptionParser
'''
僅僅是一個(gè)關(guān)于日志文件的測(cè)試,統(tǒng)計(jì)處access.log 的ip數(shù)目
'''
try:
f = open('/data/proclog/log/squid/access.log')
except IOError,e:
print "can't open the file:%s" %(e)
def log_report(field):
'''
return the field of the access log
'''
if field == "ip":
return [line.split()[2] for line in f]
if field == "url":
return [line.split()[6] for line in f]
def log_count(field):
'''
return a dict of like {field:number}
'''
fields2 = {}
fields = log_report(field)
for field_tmp in fields:
if field_tmp in fields2:
fields2[field_tmp] += 1
else:
fields2[field_tmp] = 1
return fields2
def log_sort(field,number = 10 ,reverse = True):
'''
print the sorted fields to output
'''
for v in sorted(log_count(field).iteritems(),key = lambda x:x[1] , reverse = reverse )[0:int(number)]:
print v[1],v[0]
if __name__ == "__main__":
parser =OptionParser(usage="%prog [-i|-u] [-n num | -r]" ,version = "1.0")
parser.add_option('-n','--number',dest="number",type=int,default=10,help=" print top line of the ouput")
parser.add_option('-i','--ip',dest="ip",action = "store_true",help="print ip information of access log")
parser.add_option('-u','--url',dest="url",action = "store_true",help="print url information of access log")
parser.add_option('-r','--reverse',action = "store_true",dest="reverse",help="reverse output ")
(options,args) = parser.parse_args()
if len(sys.argv) < 2:
parser.print_help()
if options.ip and options.url:
parser.error(' -i and -u can not be execute at the same time ')
if options.ip :
log_sort("ip", options.number , True and options.reverse or False)
if options.url:
log_sort("url", options.number , True and options.reverse or False)
f.close()
效果如下:


- 使用Python編寫(xiě)提取日志中的中文的腳本的方法
- python 通過(guò)logging寫(xiě)入日志到文件和控制臺(tái)的實(shí)例
- Python實(shí)現(xiàn)監(jiān)控程序執(zhí)行時(shí)間并將其寫(xiě)入日志的方法
- python寫(xiě)日志封裝類(lèi)實(shí)例
- 在Python的web框架中中編寫(xiě)日志列表的教程
- 在Python的web框架中編寫(xiě)創(chuàng)建日志的程序的教程
- Python同時(shí)向控制臺(tái)和文件輸出日志logging的方法
- python腳本實(shí)現(xiàn)統(tǒng)計(jì)日志文件中的ip訪問(wèn)次數(shù)代碼分享
- Python解析nginx日志文件
- python logging 日志輪轉(zhuǎn)文件不刪除問(wèn)題的解決方法
- 淺談python日志的配置文件路徑問(wèn)題
- python寫(xiě)日志文件操作類(lèi)與應(yīng)用示例
相關(guān)文章
用Python生成N層的楊輝三角的實(shí)現(xiàn)方法
這篇文章主要介紹了用Python生成N層的楊輝三角的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python機(jī)器學(xué)習(xí)庫(kù)之Scikit-learn基本用法詳解
Scikit-learn?是?Python?中最著名的機(jī)器學(xué)習(xí)庫(kù)之一,它提供了大量實(shí)用的機(jī)器學(xué)習(xí)算法以及相關(guān)的工具,可以方便我們進(jìn)行數(shù)據(jù)挖掘和數(shù)據(jù)分析,在這篇文章中,我們將介紹?Scikit-learn?的基本使用,包括如何導(dǎo)入數(shù)據(jù)、預(yù)處理數(shù)據(jù)、選擇和訓(xùn)練模型,以及評(píng)估模型的性能2023-07-07
python調(diào)用帶空格的windows?cmd命令問(wèn)題及連續(xù)運(yùn)行多個(gè)命令方式
這篇文章主要介紹了python調(diào)用帶空格的windows?cmd命令問(wèn)題及連續(xù)運(yùn)行多個(gè)命令方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02
使用python處理題庫(kù)表格并轉(zhuǎn)化為word形式的實(shí)現(xiàn)
這篇文章主要介紹了使用python處理題庫(kù)表格并轉(zhuǎn)化為word形式的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
python處理json文件的四個(gè)常用函數(shù)
這篇文章主要介紹了python處理json文件的四個(gè)常用函數(shù),主要包括json.load()和json.dump()及json.loads()還有json.dumps(),需要的朋友可以參考一下2022-07-07
python圖形開(kāi)發(fā)GUI庫(kù)pyqt5的基本使用方法詳解
這篇文章主要介紹了python圖形開(kāi)發(fā)GUI庫(kù)pyqt5的基本使用方法詳解,需要的朋友可以參考下2020-02-02
Python實(shí)現(xiàn)內(nèi)網(wǎng)穿透和端口轉(zhuǎn)發(fā)代理詳解
這篇文章主要為大家介紹了Python實(shí)現(xiàn)內(nèi)網(wǎng)穿透和端口轉(zhuǎn)發(fā)代理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
解決django接口無(wú)法通過(guò)ip進(jìn)行訪問(wèn)的問(wèn)題
這篇文章主要介紹了解決django接口無(wú)法通過(guò)ip進(jìn)行訪問(wèn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
解決pyecharts在jupyter notebook中使用報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了解決pyecharts在jupyter notebook中使用報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

