If you've decided that your client will have direct access to Django, you can get an IP address by simply looking at the HTTP header 'REMOTE_ADDR'
.
views.py
from django.http import HttpResponse
from django.views import View
class MyView(View):
def get(self, request):
#From the request object'REMOTE_ADDR'Get the header.
client_addr = request.META.get('REMOTE_ADDR')
#The acquired address is returned as it is to check the operation.
return HttpResponse(client_addr)
However, with the above method, if there is a reverse proxy such as Nginx between the client and Django, the IP address that can be obtained by 'REMOTE_ADDR'
will be the address of the reverse proxy (for example, Nginx is on the same host). If there is, it will be 127.0.0.1
).
If the transfer is made more than once in the middle, you can also refer to the HTTP header 'HTTP_X_FORWARDED_FOR'
to get the IP address of the transfer route as a comma-separated string, so get the client IP address from this first element. can do.
Also, if there is no 'HTTP_X_FORWARDED_FOR'
header, it is a direct access, so get it with 'REMOTE_ADDR'
as above.
views.py
from django.http import HttpResponse
from django.views import View
class MyView(View):
def get(self, request):
# 'HTTP_X_FORWARDED_FOR'Obtain the IP address of the transfer route by referring to the header.
forwarded_addresses = request.META.get('HTTP_X_FORWARDED_FOR')
if forwarded_addresses:
# 'HTTP_X_FORWARDED_FOR'If there is a header:Get the first element of the transfer route.
client_addr = forwarded_addresses.split(',')[0]
else:
# 'HTTP_X_FORWARDED_FOR'If there is no header:Because it is a direct connection'REMOTE_ADDR'Refer to the header.
client_addr = request.META.get('REMOTE_ADDR')
#The acquired address is returned as it is to check the operation.
return HttpResponse(client_addr)
Install django-ipware with pip.
python
pip3 install django-ipware
You can import it and use the get_client_ip
function to get the client's address in just one line, and of course it supports reverse proxies.
views.py
from django.http import HttpResponse
from django.views import View
from ipware import get_client_ip
class MyView(View):
def get(self, request):
client_addr, _ = get_client_ip(request)
return HttpResponse(client_addr)
The referenced HTTP header gets the client's address by default, in addition to 'HTTP_X_FORWARDED_FOR'
and 'REMOTE_ADDR'
.
python
# The default meta precedence order
IPWARE_META_PRECEDENCE_ORDER = (
'HTTP_X_FORWARDED_FOR', 'X_FORWARDED_FOR', # <client>, <proxy1>, <proxy2>
'HTTP_CLIENT_IP',
'HTTP_X_REAL_IP',
'HTTP_X_FORWARDED',
'HTTP_X_CLUSTER_CLIENT_IP',
'HTTP_FORWARDED_FOR',
'HTTP_FORWARDED',
'HTTP_VIA',
'REMOTE_ADDR',
)
You can also override the default priority with the specified priority when calling the get_client_ip
function.
python
client_addr, is_routable = get_client_ip(request, request_header_order=['X_FORWARDED_FOR', 'REMOTE_ADDR'])
Stack Overflow "How do I get user IP address in django?" https://stackoverflow.com/questions/4581789/how-do-i-get-user-ip-address-in-django
GitHub - un33k/django-ipware "A Django application to retrieve client's IP address" https://github.com/un33k/django-ipware
Recommended Posts