Skip to content

Instantly share code, notes, and snippets.

@wangtai
Created November 26, 2014 10:50
Show Gist options
  • Save wangtai/c02662f89bcd0a7b45c3 to your computer and use it in GitHub Desktop.
Save wangtai/c02662f89bcd0a7b45c3 to your computer and use it in GitHub Desktop.
def get_ip(request):
try:
real_ip = request.META['HTTP_X_FORWARDED_FOR']
reg_ip = real_ip.split(",")[0]
except KeyError:
try:
reg_ip = request.META['REMOTE_ADDR']
except KeyError:
return 0
ip_list = reg_ip.split('.')
logging.debug(repr(ip_list))
ans = 0
for item in ip_list:
ans = (ans << 8) + int(item)
return ans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment