I want to log an IP address from where particular url is accessed. I am implementing this functionality in django middleware. I tried obtaining IP address using method given in this answer:
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
I debugged this method and found that request.META.get('HTTP_X_FORWARDED_FOR') was None. and request.META.get('REMOTE_ADDR') was 10.0.0.2. However, I dont understand from where this IP 10.0.0.2 came from. I am running django inside docker container on my laptop and accessing the website iniside the browser on the same laptop using address: http://127.0.0.1:9001/. So I thought ideally the address should be at least 127.0.0.1. The IP address allotted to my laptop by router is 192.168.0.100. The website will be used inside LAN. So I feel it will be more apt to log this IP address. But I checked all keys inside request.META dictionary and found none to contain IP address 192.168.0.100. Also following two keys inside request.META contain local IP:
'HTTP_ORIGIN':'http://127.0.0.1:9001'
'HTTP_REFERER':'http://127.0.0.1:9001/'
Q1. From where the IP 10.0.0.2 obtained?
Q2. What request.META key should I access to log IP address from which the website is accessed.