Last active
August 17, 2020 12:40
-
-
Save pavlozt/8b4399b32187944f9bc7269689b195e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # like this https://mmas.github.io/read-apache-access-log-pandas | |
| import pandas as pd | |
| import io | |
| logstring=""" | |
| sosiski.ru 37.140.192.84 - - [10/Aug/2020:03:15:14 +0300] "GET /api.html?ip= HTTP/1.0" 200 4897 "-" "-" 0.137 0.082 0.082 | |
| sosiski.ru 93.77.28.57 - - [15/Aug/2020:08:43:25 +0300] "GET //%103%10%02/%08?5++]5%FF%FF%BC%FF%E3%06 HTTP/1.1" 404 43364 "https://sosiski.ru/assets/CenturySchoolbook-Italic.ttf" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36" 0.140 0.140 0.137 | |
| sosiski.ru 93.77.28.57 - - [15/Aug/2020:08:43:25 +0300] "GET //%103%10%02/%08?5++]5%FF%FF%BC%FF%E3%06 HTTP/1.1" 404 43364 "https://sosiski.ru.ru/assets/CenturySchoolbook-Italic.ttf" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36" 0.124 0.124 0.121 | |
| """ | |
| buff = io.StringIO(logstring) | |
| # Крутая функция учитывающая все временные зоны | |
| from datetime import datetime, timedelta | |
| import pytz | |
| def parse_str(x): | |
| return x[1:-1] | |
| def parse_datetime(x): | |
| print("try parse date: {}".format(x)) | |
| dt = datetime.strptime(x[1:-7], '%d/%b/%Y:%H:%M:%S') | |
| dt_tz = int(x[-6:-3])*60+int(x[-3:-1]) | |
| return dt.replace(tzinfo=pytz.FixedOffset(dt_tz)) | |
| df = pd.read_csv( | |
| buff, | |
| sep=r'\s(?=(?:[^"]*"[^"]*")*[^"]*$)(?![^\[]*\])', | |
| engine='python', | |
| na_values='-', | |
| header=None, | |
| usecols=[0,1,4,5,6,7,8,9,10], | |
| names=['host','ip', 'time', 'request', 'status', 'size', 'referer', 'user_agent','request_time'], | |
| converters={'time': parse_datetime, | |
| 'request': parse_str, | |
| 'status': int, | |
| 'size': int, | |
| 'referer': parse_str, | |
| 'user_agent': parse_str, | |
| 'request_time': float}, | |
| #error_bad_lines = False | |
| ) | |
| print(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment