Created
August 1, 2020 14:52
-
-
Save fipski/4dc76d5b67deaef52aadf58e23540658 to your computer and use it in GitHub Desktop.
compare time from nginx to a max timeout
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
#!/usr/bin/python3 | |
from datetime import datetime, timedelta | |
# sample logstring from nginx | |
logstring = '185.128.120.94 - - [01/Aug/2020:16:04:47 +0200] "POST /http-bind?room=test HTTP/2.0" 200 342 "https://foo" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.85 Safari/537.36"' | |
start = logstring.find('[')+1 | |
end = logstring.find(']') | |
time = logstring[start:end] # 01/Aug/2020:16:04:47 +0200 | |
max_diff = timedelta(minutes=15) | |
# logstring date format https://www.programiz.com/python-programming/datetime/strftime | |
date_format = "%d/%b/%Y:%H:%M:%S %z" | |
date_object = datetime.strptime(time, date_format) | |
# assume same tz as log | |
current_time = datetime.now(tz=date_object.tzinfo) | |
diff = current_time - date_object | |
print(str(diff<max_diff)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment