python正則分析nginx的訪問(wèn)日志
前言
本文的腳本是分析nginx的訪問(wèn)日志, 主要為了檢查站點(diǎn)uri的訪問(wèn)次數(shù)的,檢查的結(jié)果會(huì)提供給研發(fā)人員做參考,因?yàn)檎劦椒治雎铮强隙ㄒ玫秸齽t表達(dá)式了,所以請(qǐng)沒(méi)有接觸過(guò)正則的小伙伴自行補(bǔ)腦,因?yàn)樯婕罢齽t的內(nèi)容,實(shí)在沒(méi)法展開(kāi)寫(xiě),正則的內(nèi)容太過(guò)龐大,根本不是一篇兩篇能寫(xiě)清楚的。
開(kāi)始前,我們先看看要分析的日志結(jié)構(gòu):
127.0.0.1 - - [19/Jun/2012:09:16:22 +0100] "GET /GO.jpg HTTP/1.1" 499 0 "http://domain.com/htm_data/7/1206/758536.html" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X MetaSr 1.0)" 127.0.0.1 - - [19/Jun/2012:09:16:25 +0100] "GET /Zyb.gif HTTP/1.1" 499 0 "http://domain.com/htm_data/7/1206/758536.html" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; QQDownload 711; SV1; .NET4.0C; .NET4.0E; 360SE)"
這是修改過(guò)的日志內(nèi)容,敏感內(nèi)容都以刪除或替換了,不過(guò)不影響我們的分析結(jié)果,當(dāng)然格式什么的這都不重要,Nginx訪問(wèn)日志是可以自定義的,每家公司可能都會(huì)稍有不同,所以要能理解腳本內(nèi)容,并通過(guò)自己修改應(yīng)用到了自己工作中才是重點(diǎn),我給的日志格式也就是個(gè)參考,我打賭你在你公司服務(wù)器上看到的日志格式肯定跟我的格式不一樣, 看完日志格式,我們開(kāi)始要寫(xiě)我們的腳本了
我先貼代碼,稍后解釋?zhuān)?/strong>
import re from operator import itemgetter def parser_logfile(logfile): pattern = (r'' '(\d+.\d+.\d+.\d+)\s-\s-\s' #IP address '\[(.+)\]\s' #datetime '"GET\s(.+)\s\w+/.+"\s' #requested file '(\d+)\s' #status '(\d+)\s' #bandwidth '"(.+)"\s' #referrer '"(.+)"' #user agent ) fi = open(logfile, 'r') url_list = [] for line in fi: url_list.append(re.findall(pattern, line)) fi.close() return url_list def parser_urllist(url_list): urls = [] for url in url_list: for r in url: urls.append(r[5]) return urls def get_urldict(urls): d = {} for url in urls: d[url] = d.get(url,0)+1 return d def url_count(logfile): url_list = parser_logfile(logfile) urls = parser_urllist(url_list) totals = get_urldict(urls) return totals if __name__ == '__main__': urls_with_counts = url_count('example.log') sorted_by_count = sorted(urls_with_counts.items(), key=itemgetter(1), reverse=True) print(sorted_by_count)
腳本解釋?zhuān)?code>parser_logfile()函數(shù)功能是分析日志,返回匹配的行列表,正則部分就不解釋了,大家看注釋?xiě)?yīng)該知道它是匹配什么內(nèi)容的,parser_urllist()
函數(shù)功能是將獲取用戶訪問(wèn)的url,get_urldict()
函數(shù)功能是返回一個(gè)字典,以u(píng)rl為鍵,如果鍵相同值增1,返回的字典是每個(gè)url和最大的訪問(wèn)次數(shù),url_count()
函數(shù)功能就是調(diào)用了之前定義的函數(shù),主函數(shù)部分,就說(shuō)說(shuō)itemgetter,它可以實(shí)現(xiàn)按指定元素進(jìn)行排序,舉例就明白了:
>>> from operator import itemgetter >>> a=[('b',2),('a',1),('c',0)] >>> s=sorted(a,key=itemgetter(1)) >>> s [('c', 0), ('a', 1), ('b', 2)] >>> s=sorted(a,key=itemgetter(0)) >>> s [('a', 1), ('b', 2), ('c', 0)]
reverse=True參數(shù)表示降序排序,就是從大到小排序,腳本運(yùn)行結(jié)果:
[('http://domain.com/htm_data/7/1206/758536.html', 141), ('http://domain.com/?q=node&page=12', 3), ('http://website.net/htm_data/7/1206/758536.html', 1)]
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Nginx if語(yǔ)句加正則表達(dá)式實(shí)現(xiàn)字符串截?cái)?/a>
- Nginx的正則表達(dá)式詳解
- nginx location 配置 正則表達(dá)式實(shí)例詳解
- nginx 偽靜態(tài)Rewrite正則資源匯總
- Nginx rewrite正則匹配重寫(xiě)的方法示例
- nginx配置location總結(jié)location正則寫(xiě)法及rewrite規(guī)則寫(xiě)法
- nginx用正則表達(dá)式實(shí)現(xiàn)泛域名自動(dòng)匹配目錄的方法
- 如何利用nginx通過(guò)正則攔截指定url請(qǐng)求詳解
- Nginx正則表達(dá)式相關(guān)的參數(shù)和規(guī)則介紹
相關(guān)文章
Spyder中如何設(shè)置默認(rèn)python解釋器
Spyder作為一款流行的Python IDE,支持用戶自定義Python解釋器,包括虛擬環(huán)境的設(shè)置,通過(guò)打開(kāi)Spyder,選擇“Tools”->“Preferences”,在彈出窗口中選擇“Use the following Python interpreter”后,瀏覽并選擇相應(yīng)的解釋器或虛擬環(huán)境路徑2024-09-09詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別
這篇文章主要介紹了詳解python中 os._exit() 和 sys.exit(), exit(0)和exit(1) 的用法和區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-06-06Python numpy生成矩陣、串聯(lián)矩陣代碼分享
這篇文章主要介紹了Python numpy生成矩陣、串聯(lián)矩陣代碼分享,具有一定參考價(jià)值,需要的朋友可以了解下。2017-12-12Python中使用urllib2模塊編寫(xiě)爬蟲(chóng)的簡(jiǎn)單上手示例
這篇文章主要介紹了Python中使用urllib2模塊編寫(xiě)爬蟲(chóng)的簡(jiǎn)單上手示例,文中還介紹到了相關(guān)異常處理功能的添加,需要的朋友可以參考下2016-01-01