Skip to content

Instantly share code, notes, and snippets.

@bobycloud
Created December 24, 2019 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobycloud/470fe4dbc03274841f401dd4be868044 to your computer and use it in GitHub Desktop.
Save bobycloud/470fe4dbc03274841f401dd4be868044 to your computer and use it in GitHub Desktop.
# Get user real ip address in Python3.x
import socket
def get_real_ip_addr(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
remote_addr = request.META.get('REMOTE_ADDR')
if x_forwarded_for:
ip = x_forwarded_for.split(' ,')[0]
elif remote_addr:
ip = remote_addr
else:
ip = request.META.get('AR_REAL_IP')
return ip
ip = get_real_ip_addr(request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment