forked from Itsmmdoha/clickstat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
36 lines (30 loc) · 1.28 KB
/
utils.py
File metadata and controls
36 lines (30 loc) · 1.28 KB
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
import random
import string
def generate_identifier(length=6):
characters = string.ascii_letters
random_string = ''.join(random.choice(characters) for _ in range(length))
return random_string
def parse_and_format(results):
click_data = []
for record in results:
ip = record[0]
user_agent = record[1]
latitude = record[2]
longitude = record[3]
timestamp = record[4]
record = {"ip":ip, "user_agent":user_agent,"latitude":latitude,"longitude":longitude,"timestamp":timestamp}
click_data.append(record)
return click_data
def get_client_ip(request):
# Try to get the client IP from CF-Connecting-IP header if you are using cloudflare proxy
client_ip = request.headers.get('CF-Connecting-IP', None)
# If CF-Connecting-IP is not present, fall back to X-Forwarded-For
if client_ip is None:
x_forwarded_for = request.headers.get('X-Forwarded-For', None)
if x_forwarded_for is not None:
# X-Forwarded-For can contain a comma-separated list of IPs; take the first one
client_ip = x_forwarded_for.split(',')[0].strip()
# If both headers are missing, use the default remote address
if client_ip is None:
client_ip = request.remote_addr
return client_ip