-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
52 lines (42 loc) · 1.31 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import datetime
import os
import time
from flask import request
# IP ADDRESS AND QUERY LOGGING
ROOT_DIR = os.path.dirname(os.getcwd())
if not os.path.exists(ROOT_DIR + "/logs"):
os.makedirs(ROOT_DIR + "/logs")
fn = ROOT_DIR + "/logs/count.log"
f = open(fn, 'a+')
header = "timestamp\tIP_address\tendpoint\tquery\tpayload\n"
f.seek(0)
firstline = f.read()
if firstline == '':
f.write(header)
f.close()
def log_query(endpoint, q, payload):
if 'HTTP_X_REAL_IP' in request.environ:
addr = request.environ['HTTP_X_REAL_IP']
else:
addr = request.remote_addr
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
data = timestamp + "\t" + addr + "\t" + endpoint + "\t" + q + "\t" + str(payload) + "\n"
fi = open(fn, 'a+')
fi.write(data)
fi.close()
def ip_info(addr=''):
from urllib.request import urlopen
from json import load
if addr == '':
url = 'https://ipinfo.io/json'
else:
url = 'https://ipinfo.io/' + addr + '/json'
res = urlopen(url)
# response from url(if res==None then check connection)
data = load(res)
# will load the json response into data
for attr in data.keys():
# will print the data line by line
print(attr, ' ' * 13 + '\t->\t', data[attr])
print(data)