日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【数据平台】Python解析Ngnix日志

發布時間:2025/4/16 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【数据平台】Python解析Ngnix日志 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景:Ngnix格式具有一定格式,通過python的正則表達式讀取日志中每行的字段。

Python正則表達式參考:https://docs.python.org/2/library/re.html

代碼參考:

# -*- coding: utf-8 -*- ''' Created on 2018年1月4日@author: Jason.F @summary: Ngnix log parse ''' import time import re class NgnixLogParse(object):def __init__(self,logline):self.logline=logline'''log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr" "$request_time" "$upstream_response_time" $host';'''def logparse(self):#正則表達式ip = r"?P<ip>[\d.]*"#timelocal = r"?P<timelocal>\[.*\]" #拆解成date、month、year、log_timeuser =r"?P<user>\S+"date = r"?P<date>\d+"month = r"?P<month>\w+"year = r"?P<year>\d+"log_time = r"?P<time>\S+"#request = r"?P<request>[^\"]*" #拆解成method、request、portocalmethod = r"?P<method>\S+"request = r"?P<request>\S+"protocal=r"?P<protocal>\S+"status = r"?P<status>\d+"bodyBytesSent = r"?P<bodyBytesSent>\d+"referer=r"?P<referer>\S+"user_agent = r"?P<user_agent>[^\"]*"forwardedfor=r"?P<forwardedfor>\S+"upstream_addr = r"?P<upstream_addr>.*"requesttime=r"?P<requesttime>.*"responsetime=r"?P<responsetime>.*"host = r"?P<host>\S+"p=re.compile(r"(%s)\s-\s(%s)\s\[(%s)/(%s)/(%s)\:(%s)\s[\S]+\]\s\"(%s)\s(%s)\s(%s)\"\s(%s)\s(%s)\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s\"(%s)\"\s(%s) " \%(ip,user,date,month,year,log_time,method,request,protocal,status,bodyBytesSent,referer,user_agent,forwardedfor,upstream_addr,requesttime,responsetime,host),re.VERBOSE)m = re.findall(p, self.logline)return mif __name__ == "__main__": start = time.clock() logline='11.11.7.21 - - [22/Nov/2017:00:28:46 +0800] "POST /xyz/qn_cb HTTP/1.1" 200 218 "-" "qiniu-callback/1.0" "-" "1.25.69.11:8080" "0.008" "0.008" x.163.com'nlp=NgnixLogParse(logline)m=nlp.logparse()print (m)print (m[0][6])end = time.clock() print('finish all in %s' % str(end - start))
執行結果:

[('11.11.7.21', '-','22', 'Nov', '2017', '00:28:46', 'POST', '/xyz/qn_cb', 'HTTP/1.1', '200', '218', 'qiniu-callback/1.0', '-','1.25.69.11:8080', '0.008', '0.008', 'x.163.com')] /xyz/qn_cb finish all in 0.00115241167476


總結

以上是生活随笔為你收集整理的【数据平台】Python解析Ngnix日志的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。